0

Now I have such code:

    sortArticles = (someArray) => {
        const result = [];

        let isValid = someArray.filter(item => item.isValid);
        let isInvalid = someArray.filter(item => !item.isValid);

        valid = funcSortArrayOfObjByKey(arrayChecked, 'article');
        invalid = funcSortArrayOfObjByKey(arrayUnchecked, 'article');
        // funcSortArrayOfObjByKey - is a function to sort, a kind of lodash implementation without unnecessary dependency 

        return result.concat(valid).concat(invalid);
    }

and I want to sort such array:

[
    {
        article: 'Some New Zarticle',
        isValid: false
    },
    {
        article: 'Some New Article',
        isValid: false
    },
    {
        article: 'Some New Zzarticle',
        isValid: true
    },
]

as result I want to see such array:

[
    {
        article: 'Some New Zzarticle',
        isValid: true
    },
    {
        article: 'Some New Article',
        isValid: false
    },
    {
        article: 'Some New Zarticle',
        isValid: false
    },
]

So: I need to have some kind of two arrays: first one, and second one (one with 'isValid', other without) and both of them should be sorted by 'article'. Is this possible to do somehow? I tried also this way:

someArray.sort((a, b) => {
  if (a.isValid === b.isValid) {
    return b.isValid - a.isValid;
  } else {
    return a.article.toLowerCase().localeCompare(b.article.toLowerCase());
  }
}) 

but it's not working as I want it...

1
  • Notice that your sortArticles() function creates a new array while someArray.sort() mutates the input. Commented Sep 30, 2018 at 13:05

2 Answers 2

1

Your branches are inverted:

someArray.sort((a, b) => {
  if (a.isValid != b.isValid) {
//              ^^
    return b.isValid - a.isValid;
  } else {
    return a.article.localeCompare(b, undefined, { sensitivity: 'base' });
  }
}) 
Sign up to request clarification or add additional context in comments.

7 Comments

it won't work if, for example one of objects don't has this field (undefined) - how to deal then?
@brabertaser19 Uh, you didn't say anything about that in the question? Which field can be missing, and how do you want to treat it?
@brabertaser19 Of course the easiest way to fix it would be to keep your objects valid at all times, and simply add the field before sorting.
isValid for example can be undefined (sure, normally it's not going to happen... but)
@brabertaser19 But what? Make it to never happen. If you want the sort to handle isValid values other than true and false, you will need to adjust the sort to do it explicitly.
|
0

I was able to use this method to sort your array based on isValid then article.

someArray.sort(function(a, b) {
    if (a.isValid === b.isValid) {
        return a.article.localeCompare(b.article);
    } else {
        return (a.isValid) ? -1 : 1;
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.