简体   繁体   English

基于嵌套值或升高值的深度排序多维数组

[英]Depth sorting multidimensional array based on nested value OR elevated value

So I just had one of those "this will DEFINITELY work" moments of excitement only to be followed with severe disappointment. 因此,我只有其中一种激动人心的时刻,“这一定会成功”,但随后却感到非常失望。 I've been trying to figure out how to sort an array of arrays containing arrays for a little bit now and figured I'd see if SO wanted to help me :) 我一直在试图弄清楚如何对包含数组的数组进行排序,并弄清楚我是否想帮助我:)

Data structure: 数据结构:

var array = [{ name: "sunday",   data: [{x: 5, y: 7}, { x: 6, y: 12 }, { x: 6, y: 22 }]},
             { name: "tuesday",  data: {x: 1, y: 15}},
             { name: "monday",   data: {x: 3, y: 25}},
             { name: "thursday", data: {x: 7, y: 5}},
             { name: "friday",   data: {x: 2, y: 2}}]; 

Snippet of sorrow: 悲伤片段:

function compare(a,b) {
    var c = undefined != a.data.length ? Math.max.apply(Math,a.data.map(function(o){return o.y;})) : a.y;
    var d = undefined != b.data.length ? Math.max.apply(Math,b.data.map(function(o){return o.y;})) : b.y;

    if (c > d)
        return -1;
    if (c < d)
        return 1;
    return 0;
}

So the idea is fairly basic, I want to sort an array based on the max y value. 所以这个想法很基本,我想根据最大y值对数组进行排序。 Problem is I have a variable amount of x/y pairs for each array entry...so I need to compare based on possible deep(er) nested values. 问题是我每个数组条目都有x / y对变量...因此我需要根据可能的更深的嵌套值进行比较。

Essentially the comparer checks if data is an array or not, if it is it uses some math illusions and pops out the max value of the y's contained within...if it's not it just takes the y value. 本质上,比较器检查数据是否为数组,如果是,则使用一些数学错觉,并弹出包含在其中的y的最大值...如果不是,则仅取y值。

That works. 这样可行。 So does the extremely basic if a > b return -1 pattern I currently have implemented in the comparer. if a > b return -1我在比较器中当前实现的if a > b return -1模式,那么极其基本的if a > b return -1如此。 But SURPRISE...together they hate me. 但是惊讶...他们一起讨厌我。 Any ideas would be appreciated. 任何想法,将不胜感激。

EDIT: Failure in action - http://jsfiddle.net/TpF8g/ 编辑:操作失败-http: //jsfiddle.net/TpF8g/

So you're trying to sort a list of objects based on a property called y , and each of those objects may have a variable number of y . 因此,您正在尝试根据名为y的属性对对象列表进行排序,并且每个对象的y数量可能都是可变的。 Furthermore, if there are > 1 y , you want to consider the maximum. 此外,如果> 1 y ,则要考虑最大值。

Here's how I would write the compare function: 这是我编写比较函数的方法:

function compare(a, b) {
    // Get our maximum y value
    var getMaxY = function(data) {
        // Check if array
        if (data instanceof Array) {
            // Sort our inner array
            data.sort(function(a, b) {
                return a.y - b.y;
            });
            // Get the last one
            return data[data.length - 1].y;
        }
        // Else return y
        return data.y;
    }
    // Compare the maximum y values
    return getMaxY(a.data) - getMaxY(b.data);
}

You were just missing a data property: 您只是缺少一个data属性:

function compare(a,b) {
    var c = undefined != a.data.length ? Math.max.apply(Math,a.data.map(function(o){return o.y;})) : a.data.y;
    var d = undefined != b.data.length ? Math.max.apply(Math,b.data.map(function(o){return o.y;})) : b.data.y;

    if (c > d)
        return -1;
    if (c < d)
        return 1;
    return 0;
}

A shorter version could be: 较短的版本可能是:

function compare(a,b) {

  function byY(d1, d2){ return d1.y - d2.y; }
  // quick and dirty
  var c = a.data.length ? a.data.sort(byY)[a.data.length-1].y : a.data.y;
  var d = b.data.length ? b.data.sort(byY)[b.data.length-1].y : b.data.y;

  return d - c;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM