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

Thread: Problem with Collection sort

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with Collection sort

    Hi i just, got an error with this code; I'm trying to use sort from Collection.
    I can't see where is the problem... HELP!

    /**
     From Main: user.messages() returns an ArrayList<Message>
     
    */
    List<Message> mex = user.messages();
     
    List<Message> l = Collections.sort(mex);

    import java.util.*;
    public class Message implements Comparator<Message>{
    	private String text;
     
    	public Message(String text){
    		this.text = text;
    	}
     
    	public String toString(){
    		return this.text;
    	}
     
    	public int nChar(){
    		return text.length();
    	}
     
    	public boolean equals(Message mesEs){
    		return (this.text.equals(mesEs.text)) ? true : false;
    	}
     
    	public int compareTo(Message b){
    		if(text.compareTo(b.text)<0) return -1;
    		else if(text.compareTo(b.text)>0) return 1;
    		else return 0;
    	}
     
    	public int compare(Message a,Message b){
     
    		return a.compareTo(b);
    	}
     
    }

    Error: List<Message> l = Collections.sort(mex);
    -----------------------------------------------^
    method Collections.<T#1>sort(List<T#1>,Comparator<? super T#1>) is not applicable
    (cannot instantiate from arguments because actual and formal argument lists differ in length)
    method Collections.<T#2>sort(List<T#2>) is not applicable
    (inferred type does not conform to declared bound(s)
    inferred: Message
    bound(s): Comparable<? super Message>)
    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>)


  2. #2
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Problem with Collection sort

    Look at the definition of the Collections.sort(). Read it carefully. Then think whether your collection can be sorted, or not.

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Collection sort

    probably i did some conceptual mistake 'cose i can't fix it

  4. #4
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Problem with Collection sort

    All elements in the list must implement the Comparable interface
    This is said in javadocs I've posted.
    Now look, what interface is implemented by your Message.

  5. #5
    Junior Member
    Join Date
    May 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Collection sort

    OK
    public class Message implements Comparable<Message>{
    ...
    }

Similar Threads

  1. K-way merge sort problem
    By yotabytes in forum Algorithms & Recursion
    Replies: 0
    Last Post: March 7th, 2013, 08:12 PM
  2. collection problem
    By freakycoder in forum Collections and Generics
    Replies: 13
    Last Post: June 26th, 2012, 11:05 AM
  3. Replies: 2
    Last Post: February 17th, 2012, 01:13 AM
  4. bubble sort problem please help!!
    By yanikapausini:) in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 24th, 2012, 03:15 PM
  5. Collection.sort problem
    By Minken in forum What's Wrong With My Code?
    Replies: 12
    Last Post: September 21st, 2010, 06:24 PM

Tags for this Thread