1:00 pm - Wednesday February 22, 2017

The Collection Program Sorts the specified list into ascending order, according to the natural ordering of its elements using sort(List list)

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

 

Sorts the specified list into ascending order, according to the natural ordering of its elements.

Example Program


import java.util.*;

public class Sort {
public static void main(String[] args) {
{
List<String> col = new ArrayList<String>(); // create arraylist and
// assign it to List
col.add("red"); // add elements to arraylist
col.add("green");
col.add("yellow");
col.add("blue");
col.add("maroon");
System.out.println("Before use sorting: ");
System.out.println("\t" + col);
Collections.sort(col); // sort the string elements alphabetically
System.out.println("After sorted list: \n" + "\t" + col);// print
// sorted
// arraylist
System.out.println();
}
}
}

Output:

Before use sorting:

[red, green, yellow, blue, maroon]

After sorted list:

[blue, green, maroon, red, yellow]

Explanation:

sort(List<T> list)

Sorts the specified list into ascending order, according to the natural ordering of its elements.

Parameters:

list – the list to be sorted.

Throws:

ClassCastException–Throws if the list contains elements that are not mutually comparable (for example, strings and integers)

UnsupportedOperationException – if the specified list’s list-iterator does not support the set operation.

Since:

1.2

BACK

 

Filed in: Z-A All Java Codes

Comments are closed.