如何用同一个索引对两个数组进行排序?

时间:2021-11-23 14:05:08

I have 2 arrays. I want to sort them by same index number. For example I have these:

我有2个阵列。我想用相同的索引号对它们进行排序。例如,我有这些:

int[] a = {120, 60, 50, 40, 30, 20};
int[] b = {12, 29, 37, 85, 63, 11};

Array.Sort(b); // Now, b is -> b = {11, 12, 29, 37, 63, 85}

I want to sort a by b's index -> a = {20, 120, 60, 50, 30, 40}

我想按b的索引排序 - > a = {20,120,60,50,30,40}

If I have also string array c -> c = {"b", "u", "r", "s", "a", "1"}

如果我还有字符串数组c - > c = {“b”,“u”,“r”,“s”,“a”,“1”}

I want to sort c by b's index -> c = {"1", "b", "u", "r", "a", "s"}

我想用b的索引对c进行排序 - > c = {“1”,“b”,“u”,“r”,“a”,“s”}

How can I do this? Thanks in advance, Regards.

我怎样才能做到这一点?提前谢谢,问候。

2 个解决方案

#1


21  

Use Array.Sort<TKey, TValue>(TKey[] keys, TValue[] items) that accepts two input arrays, one is the array of keys, the other is the array of items to sort using those keys. Here, for you, b is your keys and a is your items.

使用接受两个输入数组的Array.Sort (TKey []键,TValue []项),一个是键数组,另一个是使用这些键排序的项数组。在这里,对你而言,b是你的钥匙,而a是你的物品。 ,tvalue>

Thus:

从而:

Array.Sort(b, a);

will use the keys of b to sort the items of a.

将使用b的键对a的项进行排序。

I want to sort c by b's index -> c = {"1", "b", "u", "r", "a", "s"}

我想用b的索引对c进行排序 - > c = {“1”,“b”,“u”,“r”,“a”,“s”}

Not clear exactly what you mean. At the same time as you sort a using b? If so, it's easy as we can still use the above. Zip a and c into a single array of Tuple<int, string>.

不清楚你的意思。在你使用b排序的同时?如果是这样,它很容易,因为我们仍然可以使用上述内容。将a和c压缩为单个Tuple 数组。 ,string>

var d = a.Zip(c, (x, y) => Tuple.Create(x, y)).ToArray();

Then:

然后:

Array.Sort(b, d);

as above. Then extract the pieces:

如上。然后提取件:

a = d.Select(z => z.Item1).ToArray();
c = d.Select(z => z.Item2).ToArray();

Alternatively, if you need to sort a lot of arrays using the same set of keys:

或者,如果您需要使用同一组键对许多数组进行排序:

int[] indexes = Enumerable.Range(0, b.Length).ToArray();
Array.Sort(b, indexes);

Now you can use indexes to sort all the arrays you need. For example:

现在,您可以使用索引对所需的所有阵列进行排序。例如:

a = indexes.Select(index => a[index]).ToArray();
c = indexes.Select(index => c[index]).ToArray();

etc. as needed.

等等。

Possibly some minor coding errors here. No compiler handy.

这里可能有一些小的编码错误。没有编译器方便。

#2


2  

// a dirty and inefficient way of doing it, 
// but should give you a heads up to get started

    // you obviously dont want to modify array b, so making a copy
    int[] c = Arrays.copyOf(b, b.length);
    // now apply a sort on 'c' and apply the same operation on 'a' when modifying 'c'
    // -> applying a bubble sort - > inefficient
    for( int i = 0; i < c.length ; i ++) {
        for( int j = 0 ; j < c.length - 1; j ++) {
            if(c[j] > c [j+1]) {
                c[j] = c[j] + c[j+1];
                c[j+1] = c[j] - c[j+1];
                c[j] = c[j] - c[j+1];

                // apply the same to a
                a[j] = a[j] + a[j+1];
                a[j+1] = a[j] - a[j+1];
                a[j] = a[j] - a[j+1];
            }
        }
    }

#1


21  

Use Array.Sort<TKey, TValue>(TKey[] keys, TValue[] items) that accepts two input arrays, one is the array of keys, the other is the array of items to sort using those keys. Here, for you, b is your keys and a is your items.

使用接受两个输入数组的Array.Sort (TKey []键,TValue []项),一个是键数组,另一个是使用这些键排序的项数组。在这里,对你而言,b是你的钥匙,而a是你的物品。 ,tvalue>

Thus:

从而:

Array.Sort(b, a);

will use the keys of b to sort the items of a.

将使用b的键对a的项进行排序。

I want to sort c by b's index -> c = {"1", "b", "u", "r", "a", "s"}

我想用b的索引对c进行排序 - > c = {“1”,“b”,“u”,“r”,“a”,“s”}

Not clear exactly what you mean. At the same time as you sort a using b? If so, it's easy as we can still use the above. Zip a and c into a single array of Tuple<int, string>.

不清楚你的意思。在你使用b排序的同时?如果是这样,它很容易,因为我们仍然可以使用上述内容。将a和c压缩为单个Tuple 数组。 ,string>

var d = a.Zip(c, (x, y) => Tuple.Create(x, y)).ToArray();

Then:

然后:

Array.Sort(b, d);

as above. Then extract the pieces:

如上。然后提取件:

a = d.Select(z => z.Item1).ToArray();
c = d.Select(z => z.Item2).ToArray();

Alternatively, if you need to sort a lot of arrays using the same set of keys:

或者,如果您需要使用同一组键对许多数组进行排序:

int[] indexes = Enumerable.Range(0, b.Length).ToArray();
Array.Sort(b, indexes);

Now you can use indexes to sort all the arrays you need. For example:

现在,您可以使用索引对所需的所有阵列进行排序。例如:

a = indexes.Select(index => a[index]).ToArray();
c = indexes.Select(index => c[index]).ToArray();

etc. as needed.

等等。

Possibly some minor coding errors here. No compiler handy.

这里可能有一些小的编码错误。没有编译器方便。

#2


2  

// a dirty and inefficient way of doing it, 
// but should give you a heads up to get started

    // you obviously dont want to modify array b, so making a copy
    int[] c = Arrays.copyOf(b, b.length);
    // now apply a sort on 'c' and apply the same operation on 'a' when modifying 'c'
    // -> applying a bubble sort - > inefficient
    for( int i = 0; i < c.length ; i ++) {
        for( int j = 0 ; j < c.length - 1; j ++) {
            if(c[j] > c [j+1]) {
                c[j] = c[j] + c[j+1];
                c[j+1] = c[j] - c[j+1];
                c[j] = c[j] - c[j+1];

                // apply the same to a
                a[j] = a[j] + a[j+1];
                a[j+1] = a[j] - a[j+1];
                a[j] = a[j] - a[j+1];
            }
        }
    }