Click here to Skip to main content
15,885,757 members
Articles / Programming Languages / Java

Sorting the Objects in Java

Rate me:
Please Sign up or sign in to vote.
2.56/5 (9 votes)
1 Nov 2001CPOL 73.1K   11   1
This simple program helps the user to sort objects.

Introduction

The following program helps the programmer to sort objects. This is mainly for beginners. I have used some collections classes to sort objects.

C#
public class SortObjects 
{ 
   public static void main(String s[]) 
   {  
     Collections col; 
     List l=sort(s); 
      System.out.println("\nStrings sorted List ..."); 
      for(int i=0;i<s.length;i++) 
      { 
         System.out.println((String)l.get(i)); 
      } 
      int ints[]={11,2,-22,401,6}; 
      Integer in[]=new Integer[ints.length]; 
      for(int i=0;i<in.length;i++) 
      { 
         in[i]=new Integer(ints[i]); 
      } 
      l=sort(in); 
      System.out.println("\nIntegers sorted List ..."); 
       for(int i=0;i<in.length;i++) 
       { 
         System.out.println((Integer)l.get(i)); 
       } 
    }  
    public static List sort(Object o[]) 
    { 
                 ArrayList al=new ArrayList(); 
                for(int i=0;i<o.length;i++) 
                 al.add(i,o[i]); 
                 List list = Collections.synchronizedList(al); 
                 Collections.sort(list); 
                 return list; 
   } 
}

History

  • 1st November, 2001: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
Hi
I am from South India (Hyderabad). Basically I am lover of mathematics which made me to enter computer field. I started learning computer sciences with language B.B.C Basic.
Later I changed my track to C, C++ and Java.

Comments and Discussions

 
GeneralJava Array sorting Pin
msohan0111-Nov-06 4:48
msohan0111-Nov-06 4:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.