Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Sorting the Objects in Java

0.00/5 (No votes)
1 Nov 2001 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.

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here