Heading

Wednesday, November 20, 2013

Sorting List with collection of Strings in Java

Sorting String List using Collections Sort Behaviour

package collections;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CollectionSortExample {

    public static void main(String[] args) {

    List<String> li=new ArrayList<String>();
    li.add("pavan");
    li.add("ravi");
    li.add("chaitu");
    li.add("basha");
    li.add("vasu");
    li.add("shami");
    System.out.println("Before Sorting List:    "+li);
    Collections.sort(li);
    System.out.println("\nAfter Sorting the List:"+li);
   
    }
}
O/P:
Before Sorting List:    [pavan, ravi, chaitu, basha, vasu, shami]

After Sorting the List:[basha, chaitu, pavan, ravi, shami, vasu]

No comments:

Post a Comment