0

I have two arrays, one is a 2 dimensional array, the other is a single dimensional array. I want to sort the second array in relation with the first array

Example

Input
Long [][] timeArray = {(1,2),(6,8),(3,5)};
String [] nameList = {George, Bill, Harry};

Output
timeArray = {(1,2),(3,5),(6,8)} //Array sorted in ascending order
nameList = {George, Harry, Bill} //Array sorted in relation to timeArray

I am able to sort the 2D array using

Arrays.sort(timeArray, Comparator.comparingLong(a -> a[0]));

but I am not able to figure a way to sort them in relation with each other without using over-complicated code.

2
  • 1
    Java has this neat construction called a class that allows you to group things like names and times together in one instance that can be sorted in name order or time order. Commented May 5, 2021 at 5:15
  • OMG! I just did that. Guess I was looking too hard? My Answer Commented May 6, 2021 at 1:27

3 Answers 3

1

You need to define a custom comparator which is sorting the nameList but looking at the timeArray:

List<String> list = Arrays.asList(nameList);
Comparator relationComparator = (a, b) -> {
  int indexA =list.indexOf(a);
  Int indexB = list.indexOf(b);

  return timeArray[indexA][0] - timeArray[indexB][0];
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can relate the 2 arrays (names & times) by putting them in a map, something like:

    public static void main(String[] args) {
        Map<String, Long[]> peopleTimes = new HashMap<>();
        peopleTimes.put("George", new Long[]{1L, 2L});
        peopleTimes.put("Bill", new Long[]{6L, 8L});
        peopleTimes.put("Harry", new Long[]{3L, 5L});

        Map<String, Long[]> sortedByTime = peopleTimes.entrySet().stream()
                .sorted((e1, e2) -> compareTimes(e1.getValue(), e2.getValue()))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
    }

    // You can define your compare logic or use Comparator
    public static int compareTimes(Long[] time1, Long[] time2) {
        if (time1[0] < time2[0]) return -1;
        if (time1[0] > time2[0]) return 1;
        return Long.compare(time1[1], time2[1]);
    }

Comments

1

So I figured out a way to sort do this myself. I copied the two arrays into an ArrayList of a custom class and then sorted the ArrayList using Comparator. I still think the code is hacky. Open to suggestions/improvements.

public class TimeLog{
    long startTime;
    long endTime;
    String name;
    public TimeLog(long startTime, long endTime, String name){
        super();
        this.startTime = startTime;
        this.endTime = endTime;
        this.name = name;
    }
}

In the main/function

ArrayList<TimeLog> timeLog = new ArrayList<>(); //Initializing the custom ArrayList

//Adding timeArray and nameList into custom ArrayList
for(int i = 0; i < timeArray.length; i++) {
    timeLog.add(new TimeLog(timeArray[i][0], timeArray[i][1], nameList[i]));
}

//Sorting using Array.sort and Comparator comparing start times (lambda function)
timeLog.sort(Comparator.comparingLong(st -> st.startTime));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.