【问题标题】:Javascript sort array by date and alphabetical orderJavascript按日期和字母顺序对数组进行排序
【发布时间】:2021-11-24 06:06:18
【问题描述】:

我希望它按日期和字母排序,我该怎么做? 我认为字母顺序效果很好,但日期不能正常工作。感谢您的回答。

数据结构:

[{
    productId: 21,
    title: "Huawei P40 Lite ",
    brand: "Huawei",
    price: 120,
    discountPercentage: 10,
    color: "Black",
    createdDate: "2021-01-15T01:00:00+03:00",
  },
  {
    productId: 22,
    title: "Huawei P40 Lite",
    brand: "Huawei",
    price: 1026,
    discountPercentage: 0,
    color: "Green",
    createdDate: "2021-01-16T01:00:00+03:00",
  },
  {
    productId: 23,
    title: "Apple iPhone 11",
    brand: "Apple",
    price: 1220,
    discountPercentage: 11,
    color: "White",
    createdDate: "2021-01-17T01:00:00+03:00",
  },
 {
    productId: 24,
    title: "Apple iPhone 12",
    brand: "Apple",
    price: 1420,
    discountPercentage: 11,
    color: "White",
    createdDate: "2021-01-18T01:00:00+03:00",
  }],

这是我的工作:

    jsfiddle.net/pazyqb01/

并尝试了不同的排序日期解决方案,但我无法使其工作。

排序数组应该像上面这样:

 {
    productId: 24,
    title: "Apple iPhone 12",
    brand: "Apple",
    price: 1420,
    discountPercentage: 11,
    color: "White",
    createdDate: "2021-01-18T01:00:00+03:00",
  },
{
    productId: 23,
    title: "Apple iPhone 11",
    brand: "Apple",
    price: 1220,
    discountPercentage: 11,
    color: "White",
    createdDate: "2021-01-17T01:00:00+03:00",
  },
 {
    productId: 22,
    title: "Huawei P40 Lite",
    brand: "Huawei",
    price: 1026,
    discountPercentage: 0,
    color: "Green",
    createdDate: "2021-01-16T01:00:00+03:00",
  },
{
    productId: 21,
    title: "Huawei P40 Lite ",
    brand: "Huawei",
    price: 120,
    discountPercentage: 10,
    color: "Black",
    createdDate: "2021-01-15T01:00:00+03:00",
  },

【问题讨论】:

标签: javascript arrays sorting date alphabetical


【解决方案1】:

这样:

只需按照您的排序标准列表进行操作

const data = 
  [ { productId: 21, title: 'Huawei P40 Lite ', brand: 'Huawei', price:  120, discountPercentage: 10, color: 'Black', createdDate: '2021-01-15T01:00:00+03:00' } 
  , { productId: 22, title: 'Huawei P40 Lite',  brand: 'Huawei', price: 1026, discountPercentage: 0,  color: 'Green', createdDate: '2021-01-16T01:00:00+03:00' } 
  , { productId: 23, title: 'Apple iPhone 11',  brand: 'Apple',  price: 1220, discountPercentage: 11, color: 'White', createdDate: '2021-01-17T01:00:00+03:00' } 
  , { productId: 24, title: 'Apple iPhone 12',  brand: 'Apple',  price: 1420, discountPercentage: 11, color: 'White', createdDate: '2021-01-18T01:00:00+03:00' } 
  ] 

const fSort = (a,b) =>
  {
  let Dx = new Date(b.createdDate) - new Date(a.createdDate)     // 1st criteria
  if (Dx===0) Dx = a.title.trim().localeCompare(b.title.trim()) // 2nd

  // if (Dx===0) Dx = ... // 3rd
  // if (Dx===0) Dx = ... // 4th....
  return Dx
  }

console.log( data.sort(fSort))

【讨论】:

  • 只需将其标记为duplicate。您可以将if(...) 替换为||
猜你喜欢
  • 2014-12-13
  • 2018-12-03
  • 1970-01-01
  • 1970-01-01
  • 2010-12-12
  • 1970-01-01
  • 2019-06-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多