通过不同的列取决于等于列的ID JS次要排序 [英] js secondary sort by different column depending on id of equal column

查看:159
本文介绍了通过不同的列取决于等于列的ID JS次要排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个水果采摘网站。有五个员工,人做一个AM转变,一个做了PM转变。

I have a fruit picking site. There are five staff, one does an AM shift and one does a PM shift.

在月末,看看谁是他们想排序谁(在一天的项目)拥有最最好的工人。

At the end of the month to see who is the best worker they want to sort by who has the most (items in a day).

他们想使用的辅助排序是,如果出现平局,然后他们要比较谁捡到更多的水果,他们一起工作的日子。

The secondary sort they want to use is if there is a tie then they want to compare who picked more fruit on the days where they worked together.

要在数组中处理这个问题有一个SD - ???他们用,SD每工作不同工作人员的项目 - ???通过附加的两名工作人员的ID一起,所以当SD-111222四月和布鲁诺当天确实制定。谁曾拿起多一天也得到增加这一领域的1个SD-111222。

To handle this in the array there is a sd-??? item for every different staff member they worked with, sd-??? is worked out by appending the ids of the two staff together so when sd-111222 April and Bruno did the same day. Who ever picks more for the day also gets a 1 incremented to this field sd-111222.

这是code我到目前为止看到小提琴

This is the code I have so far see fiddle.

<script>
    var workers = [
        {"id":"111","name":"April", "most":"7","sd-111222":"1","sd-111333":"2","sd-111444":"2","sd-111555":"2"},
        {"id":"222","name":"Bruno", "most":"7","sd-111222":"2","sd-222333":"1","sd-222444":"2","sd-222555":"2"},
        {"id":"333","name":"Carlos","most":"6","sd-111333":"1","sd-222333":"2","sd-333444":"2","sd-333555":"1"},
        {"id":"444","name":"Dwayne","most":"5","sd-111444":"1","sd-222444":"1","sd-333444":"1","sd-444555":"2"},
        {"id":"555","name":"Ed",    "most":"5","sd-111555":"1","sd-222555":"1","sd-333555":"2","sd-444555":"1"}
    ];

        workers.sort(function(a,b) {
            return a.most == b.most ? b.id - a.id : b.most - a.most;

        });
    console.log(workers);
</script>

进行排序按标识

我要的是类似下面的逻辑次要排序,但我不能工作,如何应用它。

What I want is something like the below logic for the secondary sort but I can't work out how to apply it.

如果最等于

获得两个相等的员工ID

get id of two equal staff

然后寻找SD - ???包含这两个ID

then look for sd-??? containing these two ids

和排序这是更大的。

所以在我的例子中,顺序应该是

So in my example the order should be

222(相当于大多数为111,但较高的SD-111222)

222 (equal most as 111 but higher sd-111222)

111

333

444(相当于大多数为555,但较高的SD-444555)

444 (equal most as 555 but higher sd-444555)

555

我怎样才能在JS应用此?

How can I apply this in js?

推荐答案

猜猜这将做的工作。

return (a.most == b.most)
    ? (b["sd-" + a.id + b.id] - a["sd-" + a.id + b.id])
    : (b.most - a.most);

修订FIDDLE

这篇关于通过不同的列取决于等于列的ID JS次要排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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