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

Posted

技术标签:

【中文标题】Javascript/Coffeescript 在值相同时对多个键上的对象数组进行排序【英文标题】:Javascript/Coffeescript Sorting an array of objects on multiple keys when the values are the same 【发布时间】:2015-06-21 14:53:25 【问题描述】:

我正在尝试按键对对象数组进行排序,但是当对象 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 数组排序的结果:

[  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  ]

【问题讨论】:

感谢您的评论,添加以澄清。我希望排序数组的第一个示例每次都会推出。 实际上,只要总是返回相同的订单,哪一个并不重要。 【参考方案1】:

您不需要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

【讨论】:

哇哦。首先,感谢您的快速回复!我是如此专注于下一个键,我什至没有想到这一点。如此明显。太感谢了!尽管如此,当初始数组的顺序不同时,我仍然会得到不同的顺序。 那么您需要发布一个显示问题的数组示例(在问题中,而不是作为评论,以获得最佳结果)。 是的,抱歉,已添加! ;)(之前没有添加,因为它们很长) 更大的问题 - 对象键没有排序。没有理由期望,每次通过for 循环,第一个key 将是date,第二个将是cpc,等等。你真的想要一个预定义的键数组,在顺序,并使用它而不是仅仅从对象中获取 key 列表。 @muistooshort 好点。用代码将其移至答案。【参考方案2】:

我本来想问这个问题,但发现有一个类似的问题,结果发现答案是我已经在做的事情,但这有一个 FOR IN 循环。 我测试了它并没有给我想要的东西 然后,当值相同时,我决定将 for in 循环换成更多代码。 这是结果

【讨论】:

以上是关于Javascript/Coffeescript 在值相同时对多个键上的对象数组进行排序的主要内容,如果未能解决你的问题,请参考以下文章

Javascript/Coffeescript/jQuery 中是不是有与 Ruby 的发送等效的内容?

确定一个数组是否包含JavaScript / CoffeeScript中另一个数组的内容[重复]

管理 Rails 5 的 javascript/coffeescript 代码的有效方法?

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

如何使用 jQuery 信用卡验证器

webpack入门02