简体   繁体   English

自定义对象排序数组

[英]Custom sorting array of objects

I have an array of objects like so for example: 我有一个像这样的对象数组:

[ {
    id: '-1'
},
{
    id: '10'
},
{
    id: '1234'
},
{
    id: '1235'
},
{
    id: '-1'
} ]

I would like to sort this array so that it is ordered by the ids ascending (smallest to largest), however I would like objects that have the id of '-1' to be sent to the back of the array. 我想对该数组进行排序,以便按ID升序排列(从最小到最大),但是我希望将ID为“ -1”的对象发送到数组的后面。 So I tried this: 所以我尝试了这个:

 const arr = [ { id: '-1' }, { id: '10'}, { id: '1234' }, { id: '1235' }, { id: '-1' } ] arr.sort((a, b) => { if (parseInt(a.id) === -1 || parseInt(b.id) === -1) return 1; if (parseInt(a.id) - parseInt(b.id)) return -1; }); console.log(arr); 

As you can see from the snippet above it sorts them successfully ascending however it doesn't successfully move ids with '-1' to the back of the array. 正如您从上面的代码片段中看到的那样,它成功地对它们进行了升序排序,但是并没有成功地将带有“ -1”的ID移到数组的后面。

Why does this happen? 为什么会这样? What am I doing wrong? 我究竟做错了什么?

You could take the function with a check for the items which should be sorted to bottom. 您可以通过检查应该排序到底部的项目来使用该功能。

 var array = [{ id: '-1' }, { id: '10' }, { id: '1234' }, { id: '1235' }, { id: '-1' }]; array.sort((a, b) => (a.id === '-1') - (b.id === '-1') || a.id - b.id); console.log(array); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

How sorting works 排序方式

First, let's take a look at what .sort expects you to feed it. 首先,让我们看一下.sort期望您提供它的内容。

Given paramaters (a, b) -- we should return 给定参数(a, b) -我们应该返回

  • < 0 if a < b 如果a <b,则< 0
  • > 0 if a > b 如果a> b > 0

How you did the sorting 您如何进行分类

Let's take a look at your first line 让我们来看看你的第一行

if (parseInt(a.id) === -1 || parseInt(b.id) === -1) return 1;

-1 means "a is smaller". -1表示“ a较小”。 But that might not be true. 但这可能不正确。 It might be that a (not b ) is -1, and is bigger, yet we're always telling JavaScript to send a to the back when that's what we should be doing to b . 可能a (不是b )是-1,并且更大,但是当我们要对b进行操作时,我们总是告诉JavaScript将a发送到后面。

Instead, we want to return -1 when b is -1, but +1 when a is -1. 相反,我们希望在b-1时返回-1 ,而在a为-1时返回+1 It doesn't really matter what we return if they're both -1. 如果它们都为-1,返回什么并不重要。



In JavaScript, any number except for 0 is truthful. 在JavaScript中,除0以外的任何数字都是真实的。 Let's take a look at your next line. 让我们来看看您的下一行。

if (parseInt(a.id) - parseInt(b.id)) return -1;

If a - b is 0, we don't return anything in this callback. 如果a - b为0,则在此回调中不返回任何内容 If it isn't 0, we always say that a < b. 如果不为0,我们总是说a <b。

Notice that never do we say that b < a -- so if such an event were to occur, we couldn't handle it and would sort it incorrectly. 请注意,我们永远不会说b <a-因此,如果发生这样的事件,我们将无法处理它并将其错误地排序。

How to sort correctly 如何正确排序

Fetch IDs 提取ID

const aID = parseInt(a.id)
const bID = parseInt(b.id)

Is a or b -1? a还是b -1?

if(aID === -1) return +1;
if(bID === -1) return -1;

Which is bigger, a or b 较大的ab

If you assume that a and b are not -1 , then we can simply subtract a - b . 如果假设ab 不为 -1 ,那么我们可以简单地减去a - b Why? 为什么? If a is bigger than b , then the subtraction will create a positive number to say b < a. 如果a大于b ,则减法将产生一个正数,表示b <a。 If a is less than b , then the subtraction will create a negative number, to say a < b. 如果a小于b ,则减法将产生一个负数,即a <b。

return aID - bID

All together 全部一起

 const arr = [ { id: '-1' }, { id: '10'}, { id: '1234' }, { id: '1235' }, { id: '-1' } ] arr.sort((a, b) => { const [aID, bID] = [parseInt(a.id), parseInt(b.id)] if(aID === -1) return +1 if(bID === -1) return -1 return aID - bID }); console.log(arr); 

Golfing 高尔夫

It might be helpful to make things shorter. 缩短内容可能会有所帮助。 Another answer, by @Nina Scholz , helpfully showed a much shorter version. @Nina Scholz的另一个答案有用地显示了一个简短得多的版本。 I thought it might be useful to explain why this works. 我认为解释一下为什么会有用。

return (a.id === '-1') - (b.id === '-1') || a.id - b.id

What is x || y 什么是x || y x || y

x || y x || y means: x || y表示:

  • if x is truthful, return x. 如果x是真实的,则返回x。
  • if x isn't truthful, return y. 如果x不真实,则返回y。

What is (aID === -1) 什么是(aID === -1)

This means true if aID is -1, and false otherwise 这意味着true ,如果援助是-1, false否则

What is (aID === -1) - (bID === -1) 什么是(aID === -1) - (bID === -1)

How can you subtract true and false ? 您如何减去truefalse true will be interpreted as 1 , and false as 0 . true将解释为1false将解释为0

aID = -1, bID = not aID = -1,bID =否

This value will be 1 - 0 , or +1 此值将是1 - 0 ,或+1

aID = not, bID = -1 aID =不,bID = -1

This value will be 0 - 1 , or -1 该值将是0 - 1-1

aID = -1, bID = -1 aID = -1,bID = -1

Remember, it doesn't matter what we return if the two values are the same 请记住,如果两个值相同,我们返回什么都没关系

aID = not, bID = not aID =不,bID =不

0 - 0 . 0 - 0 This is 0 . 这是0 This is not a truthful value. 这不是一个真实的价值。 So we go into the || 因此,我们进入|| bit. 位。 Which, in that answer, has the second bit be aID - bID , as described above. 如上所述,在该答案中第二个位是aID - bID It's very clever and very short, though might not be as readable. 它很聪明也很短,尽管可能不那么可读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM