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 13 of 13

Thread: Collections.sort

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

    Default Collections.sort

    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);
    }

    }
    }
    post.jpg

  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Collections.sort

    Please wrap your code in [code] [/code] tags. Please post the actual error message.

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

    Default Re: Collections.sort

    I am looking at other posts to see what you mean by "
     
    tags".

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Collections.sort

    Here's another look at code tags:

    [code=java]
    <YOUR CODE HERE>
    [/code]
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Collections.sort

    Thanks!

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Collections.sort

    You can edit your original post and place the tags around your posted code. Again please post the error message?

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

    Default Re: Collections.sort

    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);
     
    }
    }
    }

    The code below gives me an error at 28:
    non-static variable this cannot be referenced from a static context

    no suitable method found for sort(Collection<Integer>,gtleHw4.ListSorter)
    method Collections.<T#1>sort(List<T#1>,Comparator<? super T#1>) is not applicable
    (no instance(s) of type variable(s) T#1 exist so that argument type Collection<Integer> conforms to formal parameter type List<T#1>)
    method Collections.<T#2>sort(List<T#2>) is not applicable
    (cannot instantiate from arguments because actual and formal argument lists differ in length)
    where T#1,T#2 are type-variables:
    T#1 extends Object declared in method <T#1>sort(List<T#1>,Comparator<? super T#1>)
    T#2 extends Comparable<? super T#2> declared in method <T#2>sort(List<T#2>

    The only difference I see is the while lop reading integers from a file.
    1.	    
    2.	   public class ListSorter implements Comparator<Integer>{
    3.	       
    4.	       @Override
    5.	       public int compare(Integer o1, Integer o2){
    6.	           return (o1>o2 ? -1 : (o1==o2 ? 0 : 1));
    7.	       }
    8.	       
    9.	   }
    10.	    public static void main(String[] args)throws IOException {
    11.	        java.io.File intDat = new java.io.File("intDat");
    12.	        try (java.io.PrintWriter create = new java.io.PrintWriter (intDat)) {
    13.	            create.print("23 45 67 12 9 44 14 21 78 95 6 89 7 42 28 34");
    14.	        }
    15.	        Collection<Integer> intList;        
    16.	        try (Scanner read = new Scanner (intDat)) {
    17.	            intList = new ArrayList<>();
    18.	            //List<Integer> arrayList = new ArrayList<>();
    19.	            while (read.hasNext()){
    20.	                intList.add(read .nextInt());
    21.	                
    22.	                   
    23.	             System.out.println(intList);
    24.	            }
    25.	        }
    26.	        //System.out.println(arrayList);
    27.	           
    28.	          Collections.sort (intList, new ListSorter());
    29.	            for (Integer integer : intList){
    30.	                System.out.println (integer);
    31.	            }
    32.	            
    33.	                   }
    34.	    }
    35.	
    36.
    Last edited by gtrankle; March 24th, 2013 at 02:09 PM. Reason: Curmudgeon not happy

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Collections.sort

    Where are the line numbers showing where in the source file the errors happened?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Collections.sort

    And again, please edit your original post and add the code tags. Please don't re-ask the same question in the forum.

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

    Default Re: Collections.sort

    error at 28

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Collections.sort

    non-static variable this cannot be referenced from a static context

    error at 28
    Where is the "this" variable on line 28?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Collections.sort

    Idk??? there is no this there.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Collections.sort

    Use the javac command to get good error messages.
    Here is a sample:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. heap sort vs merge sort
    By IHeartProgramming in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 3rd, 2012, 04:37 AM
  2. 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
  3. How do I sort strings without using the sort method?
    By mjballa in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 4th, 2011, 03:27 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