user10992206
user10992206

Reputation: 13

Sort 2 arrays by the same way

I have two arrays that are not sorted: a float array (float[]), and a String array (String[]) for the descriptions.

I need to sort the float array from the highest value to the lowest, but the descriptions are in the String array and if I sort them, the String array won't be sorted accordingly.

In Processing, there is a sort(Array) function, but it only sorts one array.

How can I sort the float array and have the descriptions match?

float totalCount = 0;
float maxValue = 0;
String[] statusDescriptions = new String[finishStatusesJSON.size()];
float[] countData = new float[finishStatusesJSON.size()]; 
for (int i = 0; i < finishStatusesJSON.size(); i++) {
  JSONObject finishStatusJSON = (JSONObject) finishStatusesJSON.get(i);
  float count = finishStatusJSON.getFloat("count"); 
  String status = finishStatusJSON.getString("status");
  totalCount += count;
  statusDescriptions[i] = status;
  countData[i] = count;

  // Max value of the table
  if(maxValue < count) maxValue = count;

}

Upvotes: 1

Views: 271

Answers (3)

Prometheos II
Prometheos II

Reputation: 374

Maybe you could use a Map<K,V> type of array, where the K would be the Float class, while the V would be the String class.

Thus, you could use the put(K,V) method, like below, and then, sort properly.

float count = finishStatusJSON.getFloat("count");
totalCount += count;

map.put( count, finishStatusJSON.getString("status") );

Although, it seems like you're talking about Processing, not Java.


Erratum: It might be better to use a SortedMap implementing class; since you would be able to put a Comparator directly into the constructor, or use the natural ordering, and the Map will sort itself.

If you still want to use a Map implementing class, then I suggest that you use Map's keySet() method, then follow the same algorithm as suggested in this answer.

However, I'm skeptical of using those classes in Processing, since it might be in fact invalid for its framework; but if it compiles and there is no execution error/exception, then it may be worth a try.

Upvotes: 1

Joakim Danielson
Joakim Danielson

Reputation: 51882

How to use a simple class to hold both pieces of data and keep it in one array for easier sorting. Sorting is supported by implementing the Comparable protocol.

class Status implements Comparable<Status> {
    String  status;
    float count;

    public Status(String status, float count) {
        this.status = status;
        this.count = count;
    }

    @Override
    public int compareTo(Status s) {        
        if (this.count < s.count) {
            return 1;
        } else if (this.count > s.count) {
            return -1;
        }

        return 0;
    }
}

Status[] statusArray = new Status[finishStatusesJSON.size()];

for (int i = 0; i < finishStatusesJSON.size(); i++) {
    JSONObject finishStatusJSON = (JSONObject) finishStatusesJSON.get(i);
    Status status = new Status(finishStatusJSON.getString("status"), finishStatusJSON.getFloat("count")); 

    statusArray[i] = status;
}

Arrays.sort(statusArray);

Upvotes: 1

Yannick Buehler
Yannick Buehler

Reputation: 84

One solution would be to create a class that contains your entries, i.e. that combines a float with its description. For example, if you store movies as strings and their scores as floats, you could create a class MovieScore that contains the movie description (the string) and its score (the float). You could make this class Comparable and then sort an array of MovieScores to achieve your goal.

Upvotes: 1

Related Questions