4 Replies Latest reply: Nov 26, 2013 5:19 AM by TPD-Opitz RSS

    Sorting 2D array in jdk1.4

    AnjanN

      I have a very simple 2D array which looks like :

      42 "Zakheer"

      66 "Arnold"

      52 "Tikoyo"

      13 "Bill"

      80 "Catherine"

      I want to sort this 2D array based on col2, i.e I want the output as :

      66 "Arnold"

      13 "Bill"

      80 "Catherine"

      52 "Tikoyo"

      42 "Zakheer"

      Now my problem is I am using jdk1.4. After googling a while I found many samples, but they are using the Comparator logic that uses generics, and generics is not permitted in jdk1.4.

      So can anybody suggest other way out to achieve the same in jdk1.4.

      Any useful help is highly appreciated.

      Thanks in advance

        • 1. Re: Sorting 2D array in jdk1.4
          96491dde-8429-4baf-a153-8e33d9cfdb47

          Hi

          U can sort the strings like this.


          List<String> list = new ArrayList();
          list
          .add("development");
          list
          .add("Development");
          list
          .add("aa");
          list
          .add("AA");
          list
          .add("Aa");

           

          A common way to do it is to use comparator:

           

           

          Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
          • 2. Re: Sorting 2D array in jdk1.4
            AnjanN

            Thanks for replying...

             

            Actually my requirement is to sort the 2D array anyhow...not any collection.

            • 3. Re: Sorting 2D array in jdk1.4
              96491dde-8429-4baf-a153-8e33d9cfdb47

              public class Class6

              {

              public static void main(String[] args)

               

                String Array[][]={{"1"," Hello "} ,{"2", " This "} ,{"3", "is "},{"4", "Sorting "},{"5", "Example"}};

               

               

              for(int j=0; j<Array.length;j++)

              {

                   for (int i=j+1 ; i<Array.length; i++)

                   {

                       if(Array[i][1].compareTo(Array[j][1])<0)

                       {

                           String temp= Array[j][1];

                           Array[j][1]= Array[i][1];

                           Array[i][1]=temp;

                         String temp1= Array[j][0];

                         Array[j][1]= Array[i][0];

                         Array[i][0]=temp1;

                       }

                   }

               

              }

              }

              }

               

               

              u can refer this example and implement ur own compareTo method to compare strings.

              • 4. Re: Sorting 2D array in jdk1.4
                TPD-Opitz

                As java is an object oriented language you shoud start to see the combination on the number and the name as an object:

                class  Person implements Comparable<Person>{
                   private final int id;
                   private final String name;
                   public Person(String id, String name){
                     this.id= Integer.parseInt(id);
                     this.name=name;
                   }
                   @Override
                   public int compareTo(Person other){
                    return this.name.compareTo(other.name);
                  }
                  @Override
                  public String toString(){ return "Person[Id="+id+", name="+name+"]";}
                
                private Person[] thisShouldBetterReturnTheList(){
                String arrayIdName[][]={{"1"," Hello "} ,{"2", " This "} ,{"3", "is "},{"4", "Sorting "},{"5", "Example"}};
                   List<Person> persons= new ArrayList<Person>();
                   for(String[] arrayElement: arrayIdName){
                      persons.add(new Person(Integer.parseInt(arrayElement[0],arrayElement[1]));
                   }
                  return persons.toArray(new Person[arrayIdName.length]);
                }
                public static void main(String[] args){
                   Person[] persons=thisShouldBetterReturnTheList();
                   System.out.println(Arrays.toString(persons));
                   Arrays.sort(persons);
                   System.out.println(Arrays.toString(persons));
                  }
                }
                
                
                
                
                

                Yes, this is more code than a procedural approach, but this is just a simple example. The ratio gets much better for bigger real life applications.

                 

                And what's much more important: If you plan to add a last name which changes the "natural order" of Persons there is one single and well known place in the whole code base where you have to do the change...

                 

                bye

                TPD