Results 1 to 4 of 4

Thread: How to Sort elements of Collection

  1. #1
    Join Date
    Nov 2009
    Posts
    877

    How to Sort elements of Collection

    Hi, I want to sort elements from the collection in java. Can anyone help me to do this? If you have the program to achieve this, then please forward that to me. I am waiting for your reply. Please help me to achieve this.

  2. #2
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to Sort elements of Collection

    Hi, I have think about the sorting elements of collection, but not find any solution about this. I know how to sort array in java. If you able to get this information then please give it to me also. I am giving you the code to sort array:

    Code:
    import java.util.ArrayList;
    import java.util.Collections;
    public class SortJavaArrayListExample 
    {
    public static void main(String[] args) 
    {
    ArrayList arrayList = new ArrayList();
    arrayList.add("1");
    arrayList.add("3");
    arrayList.add("5");
    arrayList.add("2");
    arrayList.add("4");
     
    Collections.sort(arrayList);
    System.out.println("ArrayList elements after sorting in ascending order : ");
    for(int i=0; i<arrayList.size(); i++)
    System.out.println(arrayList.get(i));
    }
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,297

    Re: How to Sort elements of Collection

    Hi, I am sending you the code which will sort elements of java vector in the ascending order. Just make use of it. It will help you to sort elements.

    Code:
    import java.util.Vector; 
     import java.util.Collections;
    public class SortJavaVectorExample 
    {
     public static void main(String[] args) 
    {
     Vector v1= new Vector(); 
    v1.add("1"); 
    v1.add("3"); 
    v1.add("5"); 
    v1.add("2"); 
    v1.add("4"); 
      
    Collections.sort(v); 
    System.out.println("Vector elements after sorting in ascending order : "); 
    for(int i=0; i<v1.size(); i++) 
    System.out.println(v1.get(i)); 
    }
    }
    Output would be
    Vector elements after sorting in ascending order :
    1
    2
    3
    4
    5

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: How to Sort elements of Collection

    HI, use the following code which will help you to sort the elements of collection. Collections.sort(list) method sorts all the elements of the specified Collection. List of the elements which have to be sorted are passed through the method as a parameter. This method sorts elements in ascending order by default.

    Code:
    import java.util.*;
    import java.io.*;
    
    public class sortcoll
    {
    	public static void main(String[] args) throws IOException
    	{
    		int num = 0;
    		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    		System.out.print("How many elements you want to enter in the list : ");
    		try{
    			num = Integer.parseInt(bf.readLine());
    		}
    		catch(NumberFormatException ne){
    			System.out.println(ne.getMessage() + " is not a legal entry.");
    			System.out.println("Please enter a numeric value.");
    			System.exit(1);
    		}
    		String[] names = new String[num];
    		System.out.println("Please enter some names : ");
    		for(int i = 0; i < num; i++){
    			names[i] = bf.readLine();
    		}
    
    		List<String> list = Arrays.asList(names);
    		Collections.sort(list);
    		System.out.print("Elements of the list in sorted order : " + list);
    	}
    }

Similar Threads

  1. Replies: 3
    Last Post: 04-01-2011, 01:25 AM
  2. Sort the elements of a table in java
    By Adrina_g in forum Software Development
    Replies: 4
    Last Post: 06-03-2010, 11:59 AM
  3. How to sort LinkedList elements using java program?
    By KADRI in forum Software Development
    Replies: 4
    Last Post: 22-01-2010, 08:45 PM
  4. How to use Bubble sort in C# to sort parallel arraylists
    By Ground 0 in forum Software Development
    Replies: 3
    Last Post: 03-08-2009, 12:12 PM
  5. Photoshop Elements and Premiere Elements go to version 7.0
    By Killen in forum Customize Desktop
    Replies: 2
    Last Post: 29-08-2008, 01:17 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,114,425.66777 seconds with 17 queries