-1

I have the array of array values such as-

Object[] = [[0.25, "green"],[1.0, "red"],[0.25,"Amber"]]; 

how to sort it based on float values, my expected output will be-

Object[] = [[0.25,"green"],[0.25,"Amber"],[1.0,"Red"]];

i am implementing below code. I am trying to Assign map values into object[] array and adding to list.

   Object[] getRiskMeterColor(float val1, String val2) {
        Object[] obj = { val1, val2 };

       return obj;
   }

//This block of code is for Meter display if no values present
public List<Object[]>  getRiskMeterChartForZeroValue(List<RiskConfigTableData> classRangesList){

    List<Object[]> riskMeterColor = new ArrayList<Object[]>();
    Map<String, String> riskClassList = new TreeMap<String,String>();
    Map<String, Float> weightForLabel = new TreeMap<String,Float>();
    float riskWeight = 0;

    if(classRangesList != null && !classRangesList.isEmpty()){
        if(riskClassList != null && weightForLabel != null){
            for(Entry<String, String> colorMap : riskClassList.entrySet()){
                for(Entry<String, Float> weightMap : weightForLabel.entrySet()){
                    if(colorMap.getKey().equalsIgnoreCase(weightMap.getKey())){
                        riskMeterColor.add(getRiskMeterColor(weightMap.getValue() / 100, colorMap.getValue()));
                    }
                }
            }
        }
    }
    return riskMeterColor;
}

here i am converting that list to array so the values in side array is not coming in ascending order as shown above.

      List<Object[]> riskMeterColorList = 
       getRiskMeterChartForZeroValue(classRangesList);
        if (riskMeterColorList != null) {
        Object[] userArray = riskMeterColorList.toArray(new 
             Object[riskMeterColorList.size()]);
        }
  • 2
    Possible duplicate of [java Arrays.sort 2d array](https://stackoverflow.com/questions/15452429/java-arrays-sort-2d-array) – theblackips Apr 22 '19 at 10:59
  • As theblackips mentioned in their comment, you should have a look at [java Arrays.sort 2d array](https://stackoverflow.com/questions/15452429/java-arrays-sort-2d-array), you will find an answer on how to sort 2-dimensional Arrays. Also, in your case, i would consider using a [Map](https://docs.oracle.com/javase/8/docs/api/java/util/Map.html), because it seems you have stored key-value pairs in your arrays, which is exactly the purpose of a map. – Peter Apr 22 '19 at 12:12

1 Answers1

0

I got solution for my question myself by trying it in different ways. i am adding those two values first to list and sorting it in ascending order by using Comparator and adding this list to Object[] and it to List so it meets my need.