Results 1 to 5 of 5

Thread: How to sort items of an ArrayList in java?

  1. #1
    Join Date
    Dec 2009
    Posts
    40

    How to sort items of an ArrayList in java?

    Hello friends,
    I am last year Computer science student. I am working on live project where I use java as front end and SQL as back end. In my project in one form I have to sort items of an ArrayList. I tried various method but none of them worked out. Can anyone tell me how to sort items of an ArrayList in java?
    Thanks in advanced.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to sort items of an ArrayList in java?

    You have to just use Collections.sort() methods to sort given array list. In the following program I have use Collections.sort() methods to sort elements of an ArrayList. It is very simple program. Before using this method you have to first take input fro the user and then you have to transfer this input to the array variable. After this you can use Collections.sort() methods to sort arrayList.

    Code:
    package sample.code.java.util;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.List;
    
    public class ArrayListSortEg{
        public static void main(String[] args) {
            
            List colourss = new ArrayList();
            colours.add("reds");
            colours.add("greens");
            colours.add("blues");
            colours.add("yellows");
            colours.add("cyans");
            colours.add("whites");
            colours.add("blacks");
    
            Collections.sort(colourss);
            System.out.println(Arrays.toString(colourss.toArray()));
    
            Collections.sort(colourss, Collections.reverseOrder());
            System.out.println(Arrays.toString(colourss.toArray())); 
        }
    }

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to sort items of an ArrayList in java?

    When you store any values in ArrayList, that values are store randomly in ArrayList and to arrange all these values in proper order we have to sort it. In the following program I have use java.io.* class to accept input and show output. I have written following program for you just try to understand it. This is very simple program.

    Code:
    import java.io.*;
    import java.util.*;
     
    public class Pesort {
     
    public static void main (Strings [] argss) throws Exceptions {
     
     
     
       ArrayList <Strings> Stuffs = new ArrayList<Strings>();
     
       
     
       BufferedReader Sources = new BufferedReader(
          new FileReader("sample.txt"));
     
       System.out.println("UnSorted list ");
     
       while (Sources.ready() ) {
          String Lines = Source.readLines();   
          System.out.println(Lines);
          Stuff.add(Lines);
          }
     
       System.out.println("Alphabetic arrangement of all values");
       
       Collections.sort(Stuff);
     
       Iterator steppers = Stuff.iterator();
       while (stepper.hasNext()) {
          String currents = (String)stepper.next();
          System.out.println(currents);
          }
       
     
       System.out.println("sort by alphabetical");
     
       Collections.sort(Stuff, new byLineLengths());
     
       steppers = Stuff.iterator();
       while (steppers.hasNext()) {
          String currents = (String)steppers.next();
          System.out.println(currents);
          }
       }
     
    }

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

    Re: How to sort items of an ArrayList in java?

    It is very simple program to sort items of an ArrayList in java. You have to just use sort() method to do this. In the following program I have create one class known as "SortArrayListEg". In the following program I have use One variable to store all user value and then I have pass this variable to sort() method for sorting.


    Code:
    import java.util.*;
    
    public class SortArrayListEg
    {      
          public static void main(String s[])
          {  
                Collections col;
                List li = sort(s);
                
                System.out.println("\nStrings sorted List ...");
                for(int a = 0; a < s.length; a++)
                {
                      System.out.println((String)li.get(a));
                }
                
                int ints[] = {
                                        11, 8, -38, 752, 4
                                        };
                
                Integer ins[] = new Integer[ints.length];            
                for(int a = 0; a < in.length; a++)
                {
                      in[a] = new Integer(ints[a]);
                }
                
                li = sort(in);
                System.out.println(" sorted List of integers");
                
                for(int a = 0; a < ins.length; a++)
                {
                      System.out.println((Integer)li.get(a));
                }
        }
          
        public static List sort(Object o[])
        {
                ArrayList ali = new ArrayList();
                for(int a = 0; a < o.length; a++)
                al.add(a, o[a]);
                List listi = Collections.synchronizedList(ali);
                Collections.sort(listi);
                return listi;
        }
    }

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to sort items of an ArrayList in java?

    I have written simple program for you to to sort items of an ArrayList. Just try to understand it. This is one of the simplest program that I have written. In the following code I have create one class known as SortJavaArrayListEg to execute all method. In the following program I have create one variable to accept user input. Just try to understand each line.



    Code:
      
    
           
    
          import java.util.ArrayList;
    
          import java.util.Collections;
       
      
          public class SortJavaArrayListEg {
      
           
      
          public static void main(String[] args) {
    
           
     
    
     
          ArrayList arrayLists = new ArrayList();
    
           
     
      
    
          arrayList.add("a");
    
          arrayList.add("c");
    
          arrayList.add("e");
    
          arrayList.add("b");
    
          arrayList.add("d");
    
           
    
    
          Collections.sort(arrayList);
    
           
    
         
          System.out.println(" after sorting ArrayList elements in ascending order : ");
    
          for(int kk=0; k<arrayList.size(); k++)
    
          System.out.println(arrayList.get(k));
     
    
          }
    
          }

Similar Threads

  1. Multidimensional ArrayList in java
    By Aaliya Seth in forum Software Development
    Replies: 6
    Last Post: 16-02-2011, 01:03 AM
  2. Multidimensional ArrayList in Java
    By Navp in forum Software Development
    Replies: 1
    Last Post: 05-02-2011, 12:43 AM
  3. ArrayList Collection problem in java
    By Bricklayer in forum Software Development
    Replies: 4
    Last Post: 23-07-2010, 01:18 PM
  4. Finding items with ArrayList
    By ISAIAH in forum Software Development
    Replies: 5
    Last Post: 18-02-2010, 02:03 AM
  5. ArrayList - Sort by Index
    By TechGate in forum Software Development
    Replies: 5
    Last Post: 11-02-2010, 05:22 AM

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,713,556,806.96136 seconds with 17 queries