通过多个参数(首字母和布尔值)对对象进行排序

时间:2018-10-06 00:05:55

标签: javascript sorting

我正在尝试按以下顺序对以下对象进行排序:以sortKey的第一个字母开头的名称,然后以sortKey的第二个字母开头的名称...等等。可以将任何在sortKey中不以字母开头的名称添加到列表中。

但是如果任何个商品的销售价值为true,它们也会出现在列表的末尾。

const content = {
  Designer: "designer name",
  sortKey: ['H', 'R', 'N'],
  items: [
    {name: "Haleema", sale: false, images: Array(3), date: 1538692915225},
    {name: "Haidee", sale: false, images: Array(3), date: 1538692881303},
    {name: "Nadette", sale: false, images: Array(3), date: 1534642464516},
    {name: "Nana", sale: false, images: Array(3), date: 1534642463516},
    {name: "Nara", sale: false, images: Array(3), date: 1534642462516},
    {name: "Nativdad", sale: false, images: Array(3), date: 1534642461516},
    {name: "Noreen", sale: false, images: Array(3), date: 1534642460516},
    {name: "Nova", sale: false, images: Array(3), date: 1534642459516},
    {name: "Ada", sale: false, images: Array(3), date: 1534642458516},
    {name: "Adele", sale: false, images: Array(3), date: 1534642457516},
    {name: "Ainsley", sale: false, images: Array(3), date: 1534642456516},
    {name: "Alicia", sale: true, colour: "", size: "UK14", price: "was £1650"},
    {name: "Alison", sale: false, images: Array(3), date: 1534642454516},
    {name: "Angelica", sale: false, images: Array(3), date: 1534642453516},
    {name: "Antonia", sale: true, colour: "", size: "UK14", price: "was £2250"},
    {name: "Aryana", sale: false, images: Array(3), date: 1534642451516},
    {name: "Norma", sale: false, images: Array(2), date: 1534642450516},
    {name: "Robyn", sale: false, images: Array(3), date: 1534642449516},
    {name: "Honey", sale: false, images: Array(3), date: 1534642448516},
    {name: "Erica", sale: true, colour: "", size: "UK14", price: "was £1895"},
    {name: "Audrey", sale: false, images: Array(3), date: 1534642446516},
    {name: "Elke", sale: false, images: Array(1), date: 1534642445516},
    {name: "Eliza", sale: true, colour: "", size: "UK10", price: "was £1425"},
    {name: "Nikita", sale: false, images: Array(3), date: 1534642443516},
    {name: "Emmanuelle", sale: false, images: Array(3), date: 1534642442516},
    {name: "Helaine", sale: false, images: Array(3), date: 1534642441516},
    {name: "Nina", sale: false, images: Array(3), date: 1534642440516},
    {name: "Elma", sale: false, images: Array(3), date: 1534642439516},
    {name: "Nora", sale: false, images: Array(3), date: 1534642438516},
    {name: "Nicolette", sale: false, images: Array(3), date: 1534642437516},
    {name: "Erin", sale: false, images: Array(3), date: 1534642436516},
    {name: "Hazel", sale: false, images: Array(3), date: 1534642435516},
    {name: "Heva", sale: false, images: Array(3), date: 1534642434516},
    {name: "Ryoko", sale: true, colour: "", size: "UK12", price: "was £1495"},
    {name: "Nouvelle", sale: false, images: Array(3), date: 1534642432516},
    {name: "Noleen", sale: false, images: Array(2), date: 1534642431516},
    {name: "Hilaria", sale: false, images: Array(3), date: 1534642430516},
    {name: "Hermione", sale: false, images: Array(3), date: 1534642429516}
]};

我希望我已经解释得足够好了,在此先感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

如果您的排序顺序是对象查找而不是像这样的数组,则编码可能会更方便一些

{H: 0, R: 1, N: 2}

这将防止每次查找索引。但这仍然可行。基本思路是分类为销售/非销售,然后按sortKey中的索引排序,为找不到的对象添加一个大的排序键(因为indexOf返回-1表示找不到),然后常规字母排序:

const content = {
    Designer: "designer name",
    sortKey: ['H', 'R', 'N'],
    items: [{name: "Haleema", sale: false, images: Array(3), date: 1538692915225},{name: "Haidee", sale: false, images: Array(3), date: 1538692881303},{name: "Nadette", sale: false, images: Array(3), date: 1534642464516},{name: "Nana", sale: false, images: Array(3), date: 1534642463516},{name: "Nara", sale: false, images: Array(3), date: 1534642462516},{name: "Nativdad", sale: false, images: Array(3), date: 1534642461516},{name: "Noreen", sale: false, images: Array(3), date: 1534642460516},{name: "Nova", sale: false, images: Array(3), date: 1534642459516},{name: "Ada", sale: false, images: Array(3), date: 1534642458516},{name: "Adele", sale: false, images: Array(3), date: 1534642457516},{name: "Ainsley", sale: false, images: Array(3), date: 1534642456516},{name: "Alicia", sale: true, colour: "", size: "UK14", price: "was £1650"},{name: "Alison", sale: false, images: Array(3), date: 1534642454516},{name: "Angelica", sale: false, images: Array(3), date: 1534642453516},{name: "Antonia", sale: true, colour: "", size: "UK14", price: "was £2250"},{name: "Aryana", sale: false, images: Array(3), date: 1534642451516},{name: "Norma", sale: false, images: Array(2), date: 1534642450516},{name: "Robyn", sale: false, images: Array(3), date: 1534642449516},{name: "Honey", sale: false, images: Array(3), date: 1534642448516},{name: "Erica", sale: true, colour: "", size: "UK14", price: "was £1895"},{name: "Audrey", sale: false, images: Array(3), date: 1534642446516},{name: "Elke", sale: false, images: Array(1), date: 1534642445516},{name: "Eliza", sale: true, colour: "", size: "UK10", price: "was £1425"},{name: "Nikita", sale: false, images: Array(3), date: 1534642443516},{name: "Emmanuelle", sale: false, images: Array(3), date: 1534642442516},{name: "Helaine", sale: false, images: Array(3), date: 1534642441516},{name: "Nina", sale: false, images: Array(3), date: 1534642440516},{name: "Elma", sale: false, images: Array(3), date: 1534642439516},{name: "Nora", sale: false, images: Array(3), date: 1534642438516},{name: "Nicolette", sale: false, images: Array(3), date: 1534642437516},{name: "Erin", sale: false, images: Array(3), date: 1534642436516},{name: "Hazel", sale: false, images: Array(3), date: 1534642435516},{name: "Heva", sale: false, images: Array(3), date: 1534642434516},{name: "Ryoko", sale: true, colour: "", size: "UK12", price: "was £1495"},{name: "Nouvelle", sale: false, images: Array(3), date: 1534642432516},{name: "Noleen", sale: false, images: Array(2), date: 1534642431516},{name: "Hilaria", sale: false, images: Array(3), date: 1534642430516},{name: "Hermione", sale: false, images: Array(3), date: 1534642429516}]};
  
  content.items.sort((a,b) => {
      let aIndex = content.sortKey.indexOf(a.name[0]) 
      let bIndex = content.sortKey.indexOf(b.name[0]) 
      if (aIndex === -1) aIndex = content.sortKey.length // set sortkey to allow sorting for letters not in the sort key
      if (bIndex === -1) bIndex = content.sortKey.length
    
      return (a.sale - b.sale) || (aIndex - bIndex) || a.name.localeCompare(b.name)
  })

  console.log(content.items.map(i => ({name: i.name, sale: i.sale})))

这应该将所有sale: true项放在末尾,所有名称都不要在前面出售,并且如果sortKey在数组中,则按{{1}}排序。