How to sort two arrays in the same way but leave unsortable elements in their places?

Laba Diena

I use this code to do that:

var letters = arrayOf("one", "two", "three")
    var digits = arrayOf(2.36, 1.1, 3.0)

    val x = digits.zip(letters).toMap().toSortedMap { o1, o2 -> o2.compareTo(o1)}
    val keys = x.keys.toList()        // [3.0, 2.36, 1.1]
    val values = x.values.toList()    // [three, one, two]

But noticed one thing which is not very good. If array of digits contains more than one the same element, only the last of them is visible in keys. Is there any way to put all the same elements and leave letters elements in it's places in such case? For example: if there is [3.0, 2.36, 1.1, 1.1] in array. This code turns it to [3.0, 2.36, 1.1]. How can I solve this problem?

broot

You should not convert it to a map. Map is a data structure where keys are unique and you don't really use any features of the map here.

You can do this with the following code:

val lettersSorted = digits.zip(letters)
    .sortedByDescending { it.first }
    .map { it.second }

I'm not sure if you need a sorted list of letters only or both. And if both then whether you prefer to keep them in separate lists or in a single list. You can acquire all of these lists like this:

val sorted = digits.zip(letters)
    .sortedByDescending { it.first }  // sorted list of digits and letters

val sortedDigits = sorted.map { it.first }
val sortedLetters = sorted.map { it.second }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to sort two arrays in the same way?

How to sort arrays by two elements of these arrays?

C - Sort two arrays the same way

Is there a way to check if two arrays have the same elements?

How to sort two or three arrays at the same time?

How to leave two decimal places after the comma,

How to combine two elements in the same arrays?

How to check if two arrays contain the same elements?

How to sort some elements and leave others in Java?

Sort 2 arrays by the same way

How to compare two arrays to see how many same elements they have?

Sort two lists the same way

How to count same number of elements between two arrays, excluding duplicates

how do I average two arrays with same elements?

How to sort out elements in two-dimensional list this way?

How to print elements of two char arrays in a specific way?

How to randomize/shuffle two arrays in the same way in c#

How to sort two arrays for smoothness

How to sort two related arrays?

Is there a way to compare two arrays and return a new array of the common elements which have the same index?

Scala: Compare elements at same position in two arrays

Match two numpy arrays to find the same elements

Checks if two arrays have elements in the same order

Process elements at the same index of two arrays

Remove the same elements from two arrays Golang

Is it possible to sort the elements in an increasing way of these two variables?

How do I check in Swift if two arrays contain the same elements regardless of the order in which those elements appear in?

How to compare elements of two arrays

How to return the number of same elements on same index position when comparing two arrays