撤消在JavaScript排序的数组排序上 [英] Undo sort on sorted array in javascript

查看:141
本文介绍了撤消在JavaScript排序的数组排序上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

//data_r is an array with values

var i = 0;
var sort_order = new Array();

data_r.sort(function (a,b) {
    var res = a[0] - b[0];

    sort_order[i] = res;
    i++;

    return res;
});

在年底,SORT_ORDER数组包含当我们整理项目执行的操作。如果我要排序第二阵列正好方式和第一个相同然后我可以做到以下几点:

In the end, the sort_order array contains the actions performed when we sorted items. If I want to sort a second array exactly the same way as the first then I can do the following:

//data_x is an array with values

var i = 0;
data_x.sort(function (a,b) {
    i++;
    return sort_order[i-1];
});

现在的数据_X数组排序完全相同的方式作为data_r阵列相同的。

Now the data_x array is sorted exactly the same way as the data_r array.

现在的问题是,我该怎么样撤消data_r阵列上?

以下code是不正确的:

The following code is incorrect:

var unsort = new Array();

for(var i = 0; i < data_r.length; i++)
    unsort[i] = sort_order[i]*(-1);//-1 so we perfom the oposite action

为什么,好吗?

编辑:

我不能让数组的副本。

我有数组#1。我排序。

I have array #1. I sort it.

然后我接收阵列#2 但是数组排序基于阵列#1。

Then I receive array #2 but the array is sorted based on array #1.

我需要扭转对阵列2#排序。

I need to reverse the sorting on array #2.

编辑2:

阵列#1 = {9,5,3,0,2}

array #1 = {9, 5, 3, 0, 2}

我排序的数组#1:

阵列#1 = {0,2,3,5,9}

array #1 = {0, 2, 3, 5, 9}

现在我接收阵列#2的基础上有序数组#1:

NOW i receive array #2 sorted based on array #1:

阵列#2 = {家,车,车,PC,鼠标}

array #2 = {"home", "car", "train", "pc", "mouse"}

我需要做阵列#2是这样的:

I need to make array #2 like this:

阵列#2 = {鼠标PC,训练,家,汽车}

array #2 = {"mouse, "pc", "train", "home", "car"}

解决 http://jsfiddle.net/fQm3a/

推荐答案

请参阅@ duskwuff关于为什么你的方法是行不通的答案。

See @duskwuff's answer on why your approach doesn't work.

相反,只介绍了原始数据和排序的数据之间的映射。

Instead, just introduce a mapping between the original data and the sorted data.

{0:2, 1:3, 2:1, 3:0}

这意味着第一元件成为第三,第二成了最后等等。下面我们将使用一个数组而不是对象。

Which means the first element became the third, the second became the last and so on. Below we'll use an array instead of an object.

为什么这个地图的帮助?您可以按照它的的其他数据集,只需使用indizes它作为指针指向数据你要比较。你还可以在其他数据集轻松地应用的映射。你甚至可以扭转这种映射很容易。看到它在code:

Why does this map help? You can sort it like another dataset by just using the indizes in it as pointers to the data you're going to compare. And you can apply the mapping easily on other datasets. And you can even reverse that mapping very easily. See it in the code:

// data_r, data_x are arrays with values

var l = data_r.length;
var sort_order = new Array(l);
for (var i=0; i<l; i++) sort_order[i] = i; // initialised as 1-1 mapping

// change the sort_order first:
sort_order.sort(function (a,b) {
    // a and b being indices
    return data_r[a] - data_r[b];
});

// Making a new, sorted array
var data_x_sorted = new Array(l);
for (var i=0; i<l; i++)
    data_x_sorted[ sort_order[i] ] = data_x[i]; // put it to sorted position

如果你想在数据_X 阵列本身进行排序,只需要使用应用的算法,我表现出对 data_r

If you want to sort the data_x array itself, just use the "apply" algorithm which I showed for data_r.

现在的问题是,我该怎么样撤消 data_r 阵列上?

The question is, how can I undo sort on the data_r array?

要么根本不对它进行排序,只是做它的一个副本被排序(或者什么也不做)。

Either don't sort it at all, and just make a copy of it which gets sorted (or do nothing at all).

SORT_ORDER 扭转它。你只需要换 I newIndex 中将sortOrder [I] )无处不在。例如,对于建立一个新的未分类(旧顺序)数组:

Or use the sort_order to reverse it. You just would need to swap i and newIndex (sortOrder[i]) everywhere. Example for building a new, "unsorted" (old-order) array:

var unsorted = new Array(l);
for (var i=0; i<l; i++)
    unsorted[i] = data_r[ sort_order[i] ]; // take it from its new position

这篇关于撤消在JavaScript排序的数组排序上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆