如何在JavaScript中对数组对象进行排序?

huangapple go评论57阅读模式
英文:

How can we sort array object in JavaScript?

问题

我试图按颜色对对象数组进行排序。

我的代码:

const colors = [
  {
    "color": "purple",
    "type": true
  },
  {
    "color": "red",
    "type": false
  },
  {
    "color": "green",
    "type": true
  },
  {
    "color": "black",
    "type": false
  },
  {
    "color": "pink",
    "type": true
  }
]

const list = colors.sort((a) => a.color);
console.log(list);

如何实现这个目标?

英文:

I'm trying to sort a object array as per colors.

My code:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const colors = [
  {
    &quot;color&quot;: &quot;purple&quot;,
    &quot;type&quot;: true
   
  },
  {
    &quot;color&quot;: &quot;red&quot;,
    &quot;type&quot;: false
  },
{
    &quot;color&quot;: &quot;green&quot;,
    &quot;type&quot;: true
  },
{
    &quot;color&quot;: &quot;black&quot;,
    &quot;type&quot;: false
  },
{
    &quot;color&quot;: &quot;pink&quot;,
    &quot;type&quot;: true
  }]
  

const list = colors.sort((a) =&gt; a.color);
console.log(list);

<!-- end snippet -->

How can I achieve this?

答案1

得分: 3

此代码将帮助您根据颜色值按升序排序。

const colors = [
  {
    "color": "purple",
    "type": true
  },
  {
    "color": "red",
    "type": false
  },
  {
    "color": "green",
    "type": true
  },
  {
    "color": "black",
    "type": false
  },
  {
    "color": "pink",
    "type": true
  }
]

const sortedColors = colors.sort((a, b) => {
  if (a.color < b.color) {
    return -1;
  }
  if (a.color > b.color) {
    return 1;
  }
  return 0;
});

console.log(sortedColors);

如果您需要进一步的帮助,请随时告诉我。

英文:

This code will help you to sort in asc order based on color value.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

 const colors = [
      {
        &quot;color&quot;: &quot;purple&quot;,
        &quot;type&quot;: true
       
      },
      {
        &quot;color&quot;: &quot;red&quot;,
        &quot;type&quot;: false
      },
      {
        &quot;color&quot;: &quot;green&quot;,
        &quot;type&quot;: true
      },
      {
        &quot;color&quot;: &quot;black&quot;,
        &quot;type&quot;: false
      },
      {
        &quot;color&quot;: &quot;pink&quot;,
        &quot;type&quot;: true
      }
    ]
    
    const sortedColors = colors.sort((a, b) =&gt; {
      if (a.color &lt; b.color) {
        return -1;
      }
      if (a.color &gt; b.color) {
        return 1;
      }
      return 0;
    });
    
    console.log(sortedColors);

<!-- end snippet -->

答案2

得分: 2

尝试这个,它会起作用。

您可以根据需求更改条件,谢谢。

const colors = [{
    "color": "purple",
    "type": true
  },
  {
    "color": "red",
    "type": false
  },
  {
    "color": "green",
    "type": true
  },
  {
    "color": "black",
    "type": false
  },
  {
    "color": "pink",
    "type": true
  }
]

const result = colors.sort((a, b) => (a.color > b.color) ? 1 : ((b.color > a.color) ? -1 : 0))
console.log(result);
英文:

Try this, it will work.

You can change condition as per requirement, Thanks.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const colors = [{
    &quot;color&quot;: &quot;purple&quot;,
    &quot;type&quot;: true

  },
  {
    &quot;color&quot;: &quot;red&quot;,
    &quot;type&quot;: false
  },
  {
    &quot;color&quot;: &quot;green&quot;,
    &quot;type&quot;: true
  },
  {
    &quot;color&quot;: &quot;black&quot;,
    &quot;type&quot;: false
  },
  {
    &quot;color&quot;: &quot;pink&quot;,
    &quot;type&quot;: true
  }
]

const result = colors.sort((a, b) =&gt; (a.color &gt; b.color) ? 1 : ((b.color &gt; a.color) ? -1 : 0))
console.log(result);

<!-- end snippet -->

答案3

得分: 2

JavaScript中的Array.prototype.sort接受一个带有两个参数的回调函数。

根据MDN,

compareFn(a, b) 返回值 排序顺序
> 0 在b之后排序a
< 0 在a之前排序b
=== 0 保持a和b的原始顺序

然而,您可以使用内置的String.prototype.localeCompare来比较两个字符串,而不是编写自己的比较函数。

const colors = [
  {
    "color": "purple",
    "type": true
  },
  {
    "color": "red",
    "type": false
  },
  {
    "color": "green",
    "type": true
  },
  {
    "color": "black",
    "type": false
  },
  {
    "color": "pink",
    "type": true
  }
]

const list = colors.sort((a, b) => a.color.localeCompare(b.color));
console.log(list);
英文:

Array.prototype.sort in JavaScript takes in a callback with two parameters.

From MDN,

compareFn(a, b) return value sort order
> 0 sort a after b
< 0 sort a before b
=== 0 keep original order of a and b

However, instead of writing your own comparison function, you can use the builtin String.prototype.localeCompare to compare two strings.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const colors = [
  {
    &quot;color&quot;: &quot;purple&quot;,
    &quot;type&quot;: true
   
  },
  {
    &quot;color&quot;: &quot;red&quot;,
    &quot;type&quot;: false
  },
{
    &quot;color&quot;: &quot;green&quot;,
    &quot;type&quot;: true
  },
{
    &quot;color&quot;: &quot;black&quot;,
    &quot;type&quot;: false
  },
{
    &quot;color&quot;: &quot;pink&quot;,
    &quot;type&quot;: true
  }]
  

const list = colors.sort((a, b) =&gt; a.color.localeCompare(b.color));
console.log(list);

<!-- end snippet -->

答案4

得分: -1

Using sort(), you want to compare two elements in the array, so you need to put two parameters in there.

const colors = [
  {
    "color": "purple",
    "type": true
  },
  {
    "color": "red",
    "type": false
  },
  {
    "color": "green",
    "type": true
  },
  {
    "color": "black",
    "type": false
  },
  {
    "color": "pink",
    "type": true
  }
]

const list = colors.sort((a, b) => a.color > b.color);
console.log(list);
英文:

Using sort(), you want to compare two elements in the array, so you need to put two parameters in there.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const colors = [
  {
    &quot;color&quot;: &quot;purple&quot;,
    &quot;type&quot;: true
   
  },
  {
    &quot;color&quot;: &quot;red&quot;,
    &quot;type&quot;: false
  },
{
    &quot;color&quot;: &quot;green&quot;,
    &quot;type&quot;: true
  },
{
    &quot;color&quot;: &quot;black&quot;,
    &quot;type&quot;: false
  },
{
    &quot;color&quot;: &quot;pink&quot;,
    &quot;type&quot;: true
  }]
  

const list = colors.sort((a, b) =&gt; a.color &gt; b.color);
console.log(list);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月10日 14:53:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407800.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定