1

I'm trying to sort an array of seasons by season and year, while removing duplicate seasons. The sort is not enough I guess I need to go by a regex but could not accomplish it

const myStringArray = ["Winter 17", "Summer 13", "Winter 15", "Summer 12", "Winter 17", "Summer 12", "Summer 17"]
     
console.log(_.uniq(myStringArray).sort(function(a, b) {
    return b - a;                                  
}));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

2
  • What is the expected result? Commented Dec 29, 2017 at 9:53
  • 2
    How do you expect return b - a to work when the values are strings, not numbers? Commented Dec 29, 2017 at 9:54

3 Answers 3

2

You can try like this:

const myStringArray = ["Winter 17", "Summer 13", "Winter 15", "Summer 12", "Winter 17", "Summer 12", "Summer 17"]

console.log(_.uniq(myStringArray).sort(function(a, b) {
  return b.split(' ')[1] - a.split(' ')[1] || a.localeCompare(b);
}));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

Sign up to request clarification or add additional context in comments.

1 Comment

Works like a sharm, tkx
0

You can sort by the year, then take the index of an created array that sets the order of season. To make the array unique, simply use a new Set

const myStringArray = [...new Set(["Winter 17", "Summer 13", "Winter 15", "Summer 12", "Winter 17", "Summer 12", "Summer 17"])];
const order = ['Winter', 'Summer', 'Fall', 'Spring'];

myStringArray.sort((a, b) => {
  let s = b.split(/\s/);
  let s1 = a.split(/\s/);
  return Number(s1[1]) - Number(s[1]) || order.indexOf(s[0]) * -1;
});

console.log(myStringArray);

5 Comments

Why convert an array to a set, then convert the set back to an array?
@Barmar I don't know how else to sort a set - do you?
No, that makes no sense. Why are you creating the set in the first place, instead of just const myStringArray = ["Winter 17", "Summer 13", "Winter 15", "Summer 12", "Winter 17", "Summer 12", "Summer 17"];
@Barmar because op asks explicitly for uniqueness. Else The array isn’t unique
@Barmar ah that’s what you mean... I just prefer using built ins instead of libraries
0

If you are sure you your strings will always be in the format <Season YY> and digits will only appear once, you can use a quick and simple regex to sort desc:

const myStringArray = ["Winter 17", "Summer 13", "Winter 15", "Summer 12", "Winter 17", "Summer 12", "Summer 17"]
     
console.log(_.uniq(myStringArray).sort(function(a, b) {
    return +b.match(/\d+/)[0] - +a.match(/\d+/)[0];                                  
}));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

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.