3 Replies Latest reply: Dec 8, 2013 12:21 AM by user8696578 RSS

    sorting an arraylist in ascending order

    user8696578

      Hi All,

       

      I have an ArrayList of type String.

      The values are like below:

       

      1206 Grocery

      1107 Oil

      3001 Food

       

      So I want to show in ascending order based on the first 4 digit of the String and should yield the result as below:-

       

      1007 Oil

      1206 Grocery

      3001 Food.

       

      Thanks in advance.

        • 1. Re: sorting an arraylist in ascending order
          rp0428
          So I want to show in ascending order based on the first 4 digit of the String and should yield the result as below:-

          Great! Go right ahead. Let us know how it works out.

           

          If you need any refreshers about how to do it these two Java Tutorials should help.

          http://docs.oracle.com/javase/tutorial/collections/interfaces/order.html

          http://docs.oracle.com/javase/tutorial/collections/algorithms/

          • 2. Re: sorting an arraylist in ascending order
            96491dde-8429-4baf-a153-8e33d9cfdb47

            Hi

            U can override compareTo method of Comparable interface to sort the strings. This is how it will work.

             

            class  NumberString implements Comparable<NumberString>{ 

                private final String name; 

                public NumberString(String name){ 

                this.name=name; 

                   } 

                @Override 

                public int compareTo( NumberString other){ 

                return this.name.substring(0,4).compareTo(other.name.substring(0,4)); 

                  }

                  public String toString(){ return name;}

                private static NumberString[] thisShouldBetterReturnTheList(){ 

                String arrayIdName[]={"1012Hello " ,"1001This " ,"2103is","2002array"}; 

                   List<NumberString> numberstring= new ArrayList<NumberString>(); 

                   for(String arrayElement: arrayIdName){ 

                      numberstring.add(new NumberString(arrayElement)); 

                   } 

                  return numberstring.toArray(new NumberString[arrayIdName.length]); 

                } 

                public static void main(String[] args){ 

                   NumberString[] a1=thisShouldBetterReturnTheList();

                   Arrays.sort(a1); 

                   System.out.println(Arrays.toString(a1)); 

                  } 

                } 

             

             

            Thanks and Regards

            Sonal

            • 3. Re: sorting an arraylist in ascending order
              user8696578

              Hi user,

               

              Thanks,

              This worked.

               

              package view;

               

               

              import java.util.ArrayList;

              import java.util.Collections;

              import java.util.List;

               

               

              public class Test {

                  public Test() {

                      super();

                  }

                  private List<String> initialOrder = new ArrayList<String>();

                 

                  public static void main(String[] args) {

                      Test test = new Test();

                      test.demo();

                     

                  }

                 

                  public void demo(){

                      this.initialOrder.add("8009 Zakir");

                      this.initialOrder.add("0132 Manu");

                      this.initialOrder.add("5601 Aamir");

                      this.initialOrder.add("4006 Zaheer");

                      this.initialOrder.add("3049 Rambo");

                     

                      Collections.sort(initialOrder);

                     

                      for(int i=0;i<initialOrder.size();i++){

                          System.out.println(initialOrder.get(i));

                      }

                  }

               

               

                  public void setInitialOrder(List<String> initialOrder) {

                      this.initialOrder = initialOrder;

                  }

               

               

                  public List<String> getInitialOrder() {

                      return initialOrder;

                  }

              }

               

              Thanks....