Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java sort using anonymous class

I have class in which I am sorting a list.

import java.util.*;

public class First 
{

    private static HashMap<String,Second> msgs;

    public static void main(String[] args) 
    {           

    List<String> ls=new ArrayList<String>();


   ls.add("fourth");
   ls.add("2");
   ls.add("second");
   ls.add("first");
   ls.add("third");
   ls.add("1");

   Iterator it=ls.iterator();

   // before sorting
   while(it.hasNext())
   {
     String s=(String)it.next();
     System.out.println(s);
   }

   Collections.sort(ls, new Comparator(){
       public int compare(Object o1, Object o2) {
           return -1;
           // it can also return 0, and 1
        }
    });

System.out.println(" ");

//after sorting
   Iterator iti=ls.iterator();
   while(iti.hasNext())
   {
     String s=(String)iti.next();

     System.out.println(s);
   }

}


}

After the program is run, I get these values:

1    
third
first
second
2
fourth

My question is what is the behavior of Collection.sort() function here. On returning -1 from compare function, we get the reverse order of the list. Then how can we get other sorting orders? What is the role of returning 0, and 1?

like image 334
Osman Khalid Avatar asked May 20 '12 06:05

Osman Khalid


People also ask

How do you make an anonymous Comparator in Java?

Using anonymous classStep - 1: Create the user-defined class. Step - 2: Create the actual class where we use the Compatator object with sort method of Collections class. Step - 3: Create the object of Compatator interface using anonymous class and implement compare method of Comparator interface.

How do you use anonymous class?

Anonymous classes enable you to make your code more concise. They enable you to declare and instantiate a class at the same time. They are like local classes except that they do not have a name. Use them if you need to use a local class only once.

How do you sort a class in Java?

sort() method is present in java. util. Collections class. It is used to sort the elements present in the specified list of Collection in ascending order.

Can anonymous class be passed as a parameter?

Regular arguments may be passed to the method and are available within the anonymous class. This is good, it restricts the anonymous class to an few easy-to-identify variables and removes abominations of having to make some variables final.


2 Answers

Finally, I modified the sort function in this manner to get sorted data.

 Collections.sort(ls, new Comparator() 
                           {

                            public int compare(Object o1, Object o2) 
                            {
                            String sa = (String)o1;
                            String sb = (String)o2;

                            int v = sa.compareTo(sb);

                            return v;           

                                // it can also return 0, and 1
                            }
                           }    
                    );
like image 196
Osman Khalid Avatar answered Oct 03 '22 08:10

Osman Khalid


You can use anonymous class this way:

TreeSet<String> treeSetObj = new TreeSet<String>(new Comparator<String>() {
    public int compare(String i1,String i2)
    {
        return i2.compareTo(i1);
    }
});
like image 41
Saurabh Avatar answered Oct 03 '22 06:10

Saurabh