Javascript / Coffeescript当值相同时,对多个键上的对象数组进行排序

时间:2015-04-15 14:59:05

标签: javascript arrays sorting coffeescript

我试图按键对象数组进行排序,但是当对象a的键值与对象b的值相同时,将检查下一个键,直到它们最终不同为止。

我已尝试使用以下代码:

rows.sort (a, b) ->
   for key, value of a
      if a[key] < b[key]
        return -1
      else if a[key] > b[key]
        return 1
      else
        continue

但是当初始顺序不同时,这会导致数组看起来不同。有什么想法可以实现它吗? 谢谢!

编辑之前的数组:

[ { date: "2013-02-03",
    cpc: 1.46,
    cost: 1.46,
    clicks: 1 },
  { date: "2013-02-05",
    cpc: 1.8375,
    cost: 14.7,
    clicks: 8 },
  { date: "2013-02-06",
    cpc: 1.17,
    cost: 7.02,
    clicks: 6 },
  { date: "2013-02-09",
    cpc: 0.23,
    cost: 0.23,
    clicks: 1 },
  { date: "2013-02-15",
    cpc: 1.4949999999999999,
    cost: 2.9899999999999998,
    clicks: 2 },
  { date: "2013-02-17",
    cpc: 1.21,
    cost: 2.42,
    clicks: 2 },
  { date: "2013-02-18",
    cpc: 1.92,
    cost: 9.6,
    clicks: 5 },
  { date: "2013-02-02",
    cpc: 1.86,
    cost: 1.86,
    clicks: 1 },
  { date: "2013-02-04",
    cpc: 2.35,
    cost: 2.35,
    clicks: 1 },
  { date: "2013-02-11",
    cpc: 1.9625,
    cost: 7.85,
    clicks: 4 },
  { date: "2013-02-16",
    cpc: 2.315,
    cost: 4.63,
    clicks: 2 },
  { date: "2013-02-21",
    cpc: 1.8566666666666667,
    cost: 5.57,
    clicks: 3 },
  { date: "2013-02-22",
    cpc: 1.8375,
    cost: 7.35,
    clicks: 4 },
  { date: "2013-02-25",
    cpc: 1.5066666666666668,
    cost: 4.5200000000000005,
    clicks: 3 },
  { date: "2013-02-26",
    cpc: 2.6,
    cost: 2.6,
    clicks: 1 },
  { date: "2013-02-27",
    cpc: 2.118,
    cost: 10.59,
    clicks: 5 },
  { date: "2013-02-07",
    cpc: 0.2,
    cost: 0.2,
    clicks: 1 },
  { date: "2013-02-08",
    cpc: 1.7850000000000001,
    cost: 7.140000000000001,
    clicks: 4 },
  { date: "2013-02-10",
    cpc: 0.46,
    cost: 0.46,
    clicks: 1 },
  { date: "2013-02-14",
    cpc: 1.955,
    cost: 3.91,
    clicks: 2 },
  { date: "2013-02-19",
    cpc: 1.9233333333333331,
    cost: 17.31,
    clicks: 9 },
  { date: "2013-02-12",
    cpc: 2.39,
    cost: 7.17,
    clicks: 3 },
  { date: "2013-02-13",
    cpc: 2.53,
    cost: 2.53,
    clicks: 1 },
  { date: "2013-02-20",
    cpc: 1.87,
    cost: 1.87,
    clicks: 1 },
  { date: "2013-02-23",
    cpc: 2.265,
    cost: 9.06,
    clicks: 4 },
  { date: "2013-02-28",
    cpc: 2.2425,
    cost: 8.97,
    clicks: 4 },
  { date: "2013-03-01",
    cpc: 1.83,
    cost: 3.66,
    clicks: 2 } ]

排序后的结果(这是我期待的结果):

[ { date: '2013-02-02',
    cpc: 1.86,
    cost: 1.86,
    clicks: 1 },
  { date: '2013-02-03',
    cpc: 1.46,
    cost: 1.46,
    clicks: 1 },
  { date: '2013-02-04',
    cpc: 2.35,
    cost: 2.35,
    clicks: 1 },
  { date: '2013-02-05',
    cpc: 1.8375,
    cost: 14.7,
    clicks: 8 },
  { date: '2013-02-06',
    cpc: 1.17,
    cost: 7.02,
    clicks: 6 },
  { date: '2013-02-07',
    cpc: 0.2,
    cost: 0.2,
    clicks: 1 },
  { date: '2013-02-08',
    cpc: 1.7850000000000001,
    cost: 7.140000000000001,
    clicks: 4 },
  { date: '2013-02-09',
    cpc: 0.23,
    cost: 0.23,
    clicks: 1 },
  { date: '2013-02-10',
    cpc: 0.46,
    cost: 0.46,
    clicks: 1 },
  { date: '2013-02-11',
    cpc: 1.9625,
    cost: 7.85,
    clicks: 4 },
  { date: '2013-02-12',
    cpc: 2.39,
    cost: 7.17,
    clicks: 3 },
  { date: '2013-02-13',
    cpc: 2.53,
    cost: 2.53,
    clicks: 1 },
  { date: '2013-02-14',
    cpc: 1.955,
    cost: 3.91,
    clicks: 2 },
  { date: '2013-02-15',
    cpc: 1.4949999999999999,
    cost: 2.9899999999999998,
    clicks: 2 },
  { date: '2013-02-16',
    cpc: 2.315,
    cost: 4.63,
    clicks: 2 },
  { date: '2013-02-17',
    cpc: 1.21,
    cost: 2.42,
    clicks: 2 },
  { date: '2013-02-18',
    cpc: 1.92,
    cost: 9.6,
    clicks: 5 },
  { date: '2013-02-19',
    cpc: 1.9233333333333331,
    cost: 17.31,
    clicks: 9 },
  { date: '2013-02-20',
    cpc: 1.87,
    cost: 1.87,
    clicks: 1 },
  { date: '2013-02-21',
    cpc: 1.8566666666666667,
    cost: 5.57,
    clicks: 3 },
  { date: '2013-02-22',
    cpc: 1.8375,
    cost: 7.35,
    clicks: 4 },
  { date: '2013-02-23',
    cpc: 2.265,
    cost: 9.06,
    clicks: 4 },
  { date: '2013-02-25',
    cpc: 1.5066666666666668,
    cost: 4.5200000000000005,
    clicks: 3 },
  { date: '2013-02-26',
    cpc: 2.6,
    cost: 2.6,
    clicks: 1 },
  { date: '2013-02-27',
    cpc: 2.118,
    cost: 10.59,
    clicks: 5 },
  { date: '2013-02-28',
    cpc: 2.2425,
    cost: 8.97,
    clicks: 4 },
  { date: '2013-03-01',
    cpc: 1.83,
    cost: 3.66,
    clicks: 2 } ]

当你的订单被打扰时,初始数组:

[ { date: '2013-02-02',
    cpc: 1.86,
    cost: 1.86,
    clicks: 1 },
  { date: '2013-02-04',
    cpc: 2.35,
    cost: 2.35,
    clicks: 1 },
  { date: '2013-02-05',
    cpc: 1.8375,
    cost: 14.7,
    clicks: 8 },
  { date: '2013-02-06',
    cpc: 1.17,
    cost: 7.02,
    clicks: 6 },
  { date: '2013-02-11',
    cpc: 1.9625,
    cost: 7.85,
    clicks: 4 },
  { date: '2013-02-12',
    cpc: 2.39,
    cost: 7.17,
    clicks: 3 },
  { date: '2013-02-16',
    cpc: 2.315,
    cost: 4.63,
    clicks: 2 },
  { date: '2013-02-18',
    cpc: 1.92,
    cost: 9.6,
    clicks: 5 },
  { date: '2013-02-20',
    cpc: 1.87,
    cost: 1.87,
    clicks: 1 },
  { date: '2013-02-21',
    cpc: 1.8566666666666667,
    cost: 5.57,
    clicks: 3 },
  { date: '2013-02-28',
    cpc: 2.2425,
    cost: 8.97,
    clicks: 4 },
  { date: '2013-03-01',
    cpc: 1.83,
    cost: 3.66,
    clicks: 2 },
  { date: '2013-02-03',
    cpc: 1.46,
    cost: 1.46,
    clicks: 1 },
  { date: '2013-02-07',
    cpc: 0.2,
    cost: 0.2,
    clicks: 1 },
  { date: '2013-02-09',
    cpc: 0.23,
    cost: 0.23,
    clicks: 1 },
  { date: '2013-02-10',
    cpc: 0.46,
    cost: 0.46,
    clicks: 1 },
  { date: '2013-02-17',
    cpc: 1.21,
    cost: 2.42,
    clicks: 2 },
  { date: '2013-02-19',
    cpc: 1.9233333333333331,
    cost: 17.31,
    clicks: 9 },
  { date: '2013-02-25',
    cpc: 1.5066666666666668,
    cost: 4.5200000000000005,
    clicks: 3 },
  { date: '2013-02-08',
    cpc: 1.7850000000000001,
    cost: 7.140000000000001,
    clicks: 4 },
  { date: '2013-02-13',
    cpc: 2.53,
    cost: 2.53,
    clicks: 1 },
  { date: '2013-02-14',
    cpc: 1.955,
    cost: 3.91,
    clicks: 2 },
  { date: '2013-02-15',
    cpc: 1.4949999999999999,
    cost: 2.9899999999999998,
    clicks: 2 },
  { date: '2013-02-22',
    cpc: 1.8375,
    cost: 7.35,
    clicks: 4 },
  { date: '2013-02-23',
    cpc: 2.265,
    cost: 9.06,
    clicks: 4 },
  { date: '2013-02-26',
    cpc: 2.6,
    cost: 2.6,
    clicks: 1 },
  { date: '2013-02-27',
    cpc: 2.118,
    cost: 10.59,
    clicks: 5 }]

使用hussled array进行排序的结果:

[ { date: '2013-02-02',
    cpc: 1.86,
    cost: 1.86,
    clicks: 1 },
  { date: '2013-02-03',
    cpc: 1.46,
    cost: 1.46,
    clicks: 1 },
  { date: '2013-02-04',
    cpc: 2.35,
    cost: 2.35,
    clicks: 1 },
  { date: '2013-02-05',
    cpc: 1.8375,
    cost: 14.7,
    clicks: 8 },
  { date: '2013-02-06',
    cpc: 1.17,
    cost: 7.02,
    clicks: 6 },
  { date: '2013-02-07',
    cpc: 0.2,
    cost: 0.2,
    clicks: 1 },
  { date: '2013-02-08',
    cpc: 1.7850000000000001,
    cost: 7.140000000000001,
    clicks: 4 },
  { date: '2013-02-10',
    cpc: 0.46,
    cost: 0.46,
    clicks: 1 },
  { date: '2013-02-11',
    cpc: 1.9625,
    cost: 7.85,
    clicks: 4 },
  { date: '2013-02-12',
    cpc: 2.39,
    cost: 7.17,
    clicks: 3 },
  { date: '2013-02-13',
    cpc: 2.53,
    cost: 2.53,
    clicks: 1 },
  { date: '2013-02-14',
    cpc: 1.955,
    cost: 3.91,
    clicks: 2 },
  { date: '2013-02-15',
    cpc: 1.4949999999999999,
    cost: 2.9899999999999998,
    clicks: 2 },
  { date: '2013-02-16',
    cpc: 2.315,
    cost: 4.63,
    clicks: 2 },
  { date: '2013-02-17',
    cpc: 1.21,
    cost: 2.42,
    clicks: 2 },
  { date: '2013-02-18',
    cpc: 1.92,
    cost: 9.6,
    clicks: 5 },
  { date: '2013-02-19',
    cpc: 1.9233333333333331,
    cost: 17.31,
    clicks: 9 },
  { date: '2013-02-20',
    cpc: 1.87,
    cost: 1.87,
    clicks: 1 },
  { date: '2013-02-21',
    cpc: 1.8566666666666667,
    cost: 5.57,
    clicks: 3 },
  { date: '2013-02-22',
    cpc: 1.8375,
    cost: 7.35,
    clicks: 4 },
  { date: '2013-02-23',
    cpc: 2.265,
    cost: 9.06,
    clicks: 4 },
  { date: '2013-02-09',
    cpc: 0.23,
    cost: 0.23,
    clicks: 1 },
  { date: '2013-02-25',
    cpc: 1.5066666666666668,
    cost: 4.5200000000000005,
    clicks: 3 },
  { date: '2013-02-26',
    cpc: 2.6,
    cost: 2.6,
    clicks: 1 },
  { date: '2013-02-27',
    cpc: 2.118,
    cost: 10.59,
    clicks: 5 },
  { date: '2013-02-28',
    cpc: 2.2425,
    cost: 8.97,
    clicks: 4 },
  { date: '2013-03-01',
    cpc: 1.83,
    cost: 3.66,
    clicks: 2 } ]

2 个答案:

答案 0 :(得分:3)

您不需要continue。并确保在对象匹配时返回0

rows.sort (a, b) ->
  for key, value of a
    if a[key] < b[key]
      return -1
    else if a[key] > b[key]
      return 1

  return 0

更大的问题 - 对象键没有排序。没有理由期望,每次通过for循环,第一个键将是日期,第二个键将是cpc等。你真的想要按顺序拥有预定义的键数组,并使用而不只是从对象中获取键列表

keys = ["a","b","c"]

rows.sort (a, b) ->
  for key in keys
    if a[key] < b[key]
      return -1
    else if a[key] > b[key]
     return 1

  return 0

Example

答案 1 :(得分:0)

我正要问这个问题,但发现有一个类似的问题,原来答案是我已经在做的事,但这有一个FOR IN循环。 我测试了一下,没有给我想要的东西 然后,我决定在值相同时换出for in循环以获取更多代码。 这是结果

Looping through the given keys and comparing all

The loop outcome

Comparing a different key  value pair when the initial values compared are the same

The outcome