4 Replies Latest reply: Nov 6, 2007 12:58 PM by DrClap RSS

    sorting array double or int value and keeping the associated identifier.

    807603
      Hi,

      How would I sort an array which contains a double or int value (I know how to do that) but I also want to keep the unique identifier which is associated to array element after the array has been sorted:

      example:
      identifier: 15STH7425042735
      double: 742500.000

      Thanks,

      John J. Mitchell
        • 1. Re: sorting array double or int value and keeping the associated identifier
          807603
          Don't use two arrays. Use one array (or java.util.List) that hold objects that hold the pair of associated values.
          • 2. Re: sorting array double or int value and keeping the associated identifier
            rahul_akkina
            Please define an it in form of entity first and then think of operating on structured data.

            a better approach here would be please create an instances of appropriate structured data arrange them in form of a list and then apply specified logic on them

            here is an example which you might think of implementing it and would more appropriate as per your stated requirements.

            public class SampleBean implements Serializable{
            
               private String uniqueIdentifier = new String();
               private double appValue = 0.0;
            
               public void setUniqueIdentifier(String uniqueIdentifier){
                   this.uniqueIdentifier = uniqueIdentifier;
               }
            
               public String getUniqueIdentifier(){
                  return this.uniqueIdentifier;
               } 
            
               public void setAppValue(double appValue){
                      this.appValue = appValue;
               }
            
               public double getAppValue(){
                    return this.appValue;
               }
            
            }
            Sample Comparators:
            Comparator comparator4AppValue = new Comparator(){
            
                  public int compare(Object obj1,Object obj2){ 
                          return Double.compare(((SampleBean)obj1).getAppValue(),((SampleBean)obj2).getAppValue()); 
                   }
            
            };
            
            
            Comparator comparator4UniqueIdentifier = new Comparator(){
            
                  public int compare(Object obj1,Object obj2){ 
                          return ((SampleBean)obj1).getUniqueIdentifier().compareTo( ((SampleBean)obj2).getUniqueIdentifier()); 
                   }
            
            };
            Assuming that you have acquired an Array or List 'N' Elements of SampleBean with appropriate values.
            the belows is the method of how to sort specified Array/List


            In case of array
            SampleBean sb[] = this.getSampleBeansArray();
            
            // in order to sort using double value inside SampleBean array.
            Arrays.sort(sb,comparator4AppValue);
            
            // in order to sort using unique Identifier value inside SampleBean array.
            Arrays.sort(sb,comparator4UniqueIdentifier);
            In case of a list
            List sb = this.getSampleBeansList();
            
            // in order to sort using double value inside SampleBean array.
            Collections.sort(sb,comparator4AppValue);
            
            // in order to sort using unique Identifier value inside SampleBean array.
            Collections.sort(sb,comparator4UniqueIdentifier);
            Hope this might give you some idea of how to go about.:)

            REGARDS,
            RaHuL
            • 3. Re: sorting array double or int value and keeping the associated identifier
              807603
              Hi RaHuL,

              Thanks for the information I forgot to mention something in that I need to sort not only by a double value (like Longitude) but also within each double value I need to sort by a different double value (Like Latitude) within that. I don't need to sort by the unique identifier but just need to reference its value once the 2 level sort has completed.

              example:
              identifier (String): 15STH7425042735
              Longitude (double): 742500.000
              Latutude (double): 4275000.000

              Thanks,

              John J. Mitchell
              • 4. Re: sorting array double or int value and keeping the associated identifier
                DrClap
                Okay. So store all three of those things in your object and use them to have your comparator do whatever you meant by that. Rahul gave you a framework to work with, if it's not your precise requirements then change it until it is.