0

I have a method which, based on some JSON creates two arrays: an index of array of type int

int[] indices;

and a relevance array of type double

double[] relevance;

The two arrays are guaranteed to have the same size after being set. I need to retrieve the sorted array of indices, based on the values from the relevance array. Example:

indices = {5, 8, 3, 2}
relevance = {0.1234, 0.3567, 0.2254, 0.0005}

The returned result would be:

{2, 5, 3, 8}

My solution, for the time being, is to use a custom sorting function (Bubble sort) which compares values of relevance array and swaps the values in both the relevance and the indices array.

Is there a more stylish approach to this problem?

5
  • There is a CodeReview Exchange. Ask it there. codereview.stackexchange.com Commented Dec 3, 2018 at 10:50
  • Take a look on TreeMap, where values from relevance will be key, and values from indices will be value Commented Dec 3, 2018 at 10:58
  • Map<Double, Integer> accumulator = new TreeMap<>(Comparator.naturalOrder()); accumulator.put(0.1234, 5); accumulator.put(0.3567, 8); accumulator.put(0.2254, 3); accumulator.put(0.0005, 2); System.out.println(accumulator); Commented Dec 3, 2018 at 11:03
  • 1
    @MuratKaragöz this doesn't really strike me as a code review question Commented Dec 3, 2018 at 11:18
  • I did consider stack exchange but I do not provide any actual implementation so I don't think it would be suitable as a review question. However, I acknowledged it is a duplicate since the data types of the arrays do not actually matter and there is a question already for 2 string arrays. Commented Dec 3, 2018 at 11:23

1 Answer 1

1

You can create an object holding both relevance and index at the same time and put that object in a new List. Now you can sort that list by relevance and get the corresponding indexes.

Something like that:

// Class holding relevance and index at the same time
public class RelevanceIndex {
  private int index;
  private double relevance;
  ...
}

// Create and populate a list of RelevanceIndex
List<RelevanceIndex> relevanceIndexes = new ArrayList<>();
for (int i = 0; i < indices.length; i++) {
  RelevanceIndex relevanceIndex = new RelevanceIndex();
  relevanceIndex.setIndex(indexes[i]);
  relevanceIndex.setRelevance(relevances[i]);
  relevanceIndexes.add(relevanceIndex);
}

...
// Sort relevanceIndexes by relevance using method sort of List
// (you need to define your Comparator or define RelevanceIndex as
// Comparable)
// Now you have the related indexes sorted. If necessary you can put them 
// in a new sorted array

EDIT: Added a complete implementation of this answer

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ArraySorting {

    public static void main(String[] args) {

        int[] indices = {5, 8, 3, 2};
        double[] relevance = {0.1234, 0.3567, 0.2254, 0.0005};

        ArraySorting app = new ArraySorting();
        app.run(indices, relevance);
    }

    void run(int[] indices, double[] relevance) {
        List<RelevanceIndex> relevanceIndices = getRelevanceIndices(indices, relevance);

        System.out.println(relevanceIndices);
        Collections.sort(relevanceIndices);
        System.out.println(relevanceIndices);
    }

    List<RelevanceIndex> getRelevanceIndices(int[] indices, double[] relevance) {
        List<RelevanceIndex> relevanceIndices = new ArrayList<>();
        for (int i = 0; i < indices.length; i++) {
            relevanceIndices.add(new RelevanceIndex(indices[i], relevance[i]));
        }
        return relevanceIndices;
    }

    class RelevanceIndex implements Comparable<RelevanceIndex> {
        private int index;
        private double relevance;

        RelevanceIndex(int index, double relevance) {
            this.index = index;
            this.relevance = relevance;
        }

        @Override
        public int compareTo(RelevanceIndex other) {
            return Double.compare(this.relevance, other.relevance);
        }

        @Override
        public String toString() {
            return String.format("%s (%s)", index, relevance);
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the effort! I had used Collections before but did not see the answer in this situation so clearly.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.