Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Collections.sort

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Collections.sort

    I will try again.
    This compiles and runs well:
    public class IntegerTest implements Comparator<Integer>{
    @Override
    public int compare(Integer o1, Integer o2){
    return (o1<o2 ? -1 : (o1==o2 ? 0 : 1));
    }
     
     
    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    List<Integer> arrayList = new ArrayList<>();
    arrayList.add(12);
    arrayList.add(5);
    arrayList.add(7);
    arrayList.add(4);
    arrayList.add(5);
    arrayList.add(16);
    arrayList.add(13);
    arrayList.add(2);
    System.out.println(arrayList);
    Collections.sort (arrayList, new IntegerTest());
    for (Integer integer : arrayList){
    System.out.println (integer);
     
    }
    }
    }
    This code creates and prints the array but gives me an error at Collections.sort.
    What is wrong with the call?
    public class ListSorter implements Comparator<Integer>{
     
    @Override
    public int compare(Integer o1, Integer o2){
    return (o1>o2 ? -1 : (o1==o2 ? 0 : 1));
    }
     
    }
    public static void main(String[] args)throws IOException {
    java.io.File intDat = new java.io.File("intDat");
    try (java.io.PrintWriter create = new java.io.PrintWriter (intDat)) {
    create.print("23 45 67 12 9 44 14 21 78 95 6 89 7 42 28 34");
    }
    Collection<Integer> intList;
    try (Scanner read = new Scanner (intDat)) {
    intList = new ArrayList<>();
    //List<Integer> arrayList = new ArrayList<>();
    while (read.hasNext()){
    intList.add(read .nextInt());
     
     
    System.out.println(intList);
    }
    }
    //System.out.println(arrayList);
     
    Collections.sort (intList, new ListSorter());
    for (Integer integer : intList){
    System.out.println (integer);
    }
     
    }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,437
    Thanks
    49
    Thanked 2,217 Times in 2,190 Posts

    Default Re: Collections.sort

    gives me an error at
    Please copy the full text of the error message and paste it here.

    BTW The posted code needs formatting. Nested statements should be indented.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Collections.sort

    Check the java docs for Collections.sort(). It does not work on a Collection even though you instantiated it as an ArrayList. You should declare it as a List instead of Collection.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  4. The Following User Says Thank You to Chris.Brown.SPE For This Useful Post:

    poundnmonitor (March 30th, 2013)

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,437
    Thanks
    49
    Thanked 2,217 Times in 2,190 Posts

    Default Re: Collections.sort

    The posted code has compiler errors as described in post#3
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    poundnmonitor (March 30th, 2013)

Similar Threads

  1. Collections.sort
    By gtrankle in forum Object Oriented Programming
    Replies: 12
    Last Post: March 24th, 2013, 02:35 PM
  2. heap sort vs merge sort
    By IHeartProgramming in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 3rd, 2012, 03:37 AM
  3. How to call a C sort function to sort a Java Array.
    By Dwere13 in forum Java Native Interface
    Replies: 22
    Last Post: July 12th, 2012, 04:44 PM
  4. how to sort objects with collections???
    By kyros in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 31st, 2010, 02:21 PM
  5. bubble sort and selection sort on strings
    By Sir Saula in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 3rd, 2010, 09:44 AM