/    Sign up×
Community /Pin to ProfileBookmark

How to sort list of resolutions correctly

I have a list of resolution strings like `2160p`, `1080p`, `720p`, `480p` that I’d like to be sorted from biggest to smallest. I’ve tried removing the trailing `p` using `replace(…)` and then simply sorting them, but it still ends up putting `2160p` after `1080p` for example.

I have the same system working for alphabetically sorting resolution strings like `UHD`, `FHD`, `HD` but want to support both.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumMay 18.2020 — Try to convert the values by parseInt, then you need not to remove the trailing `p`.
Copy linkTweet thisAlerts:
@DesertCookieauthorMay 18.2020 — @Sempervivum#1618553 Thanks for the suggestion. It sorts everything correctly, except for 2160p, which it puts at the end of the list, right after 360p :/ .
Copy linkTweet thisAlerts:
@SempervivumMay 18.2020 — I tested it by use of this code:
let res = ['2160p', '1080p', '720p', '480p'];
res.sort((a, b) => {
return (parseInt(a) - parseInt(b));
});
console.log(res);
and it works fine for me. If it does not for you, please post your code.
Copy linkTweet thisAlerts:
@DesertCookieauthorMay 19.2020 — It seems I had to wait a couple of problems for the changes to take effect. Just reloading the page and clearing the cache didn't do the trick. (Firefox and Webpage via localhost).
×

Success!

Help @DesertCookie spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.30,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...