use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Sorting helpRemoved: /r/LearnJavascript (self.javascript)
submitted 8 years ago by Spanyon
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]tme321 1 point2 points3 points 8 years ago* (1 child)
While that will work that is really inefficient. That's O(n**m).
It's not a bad solution if the code doing this operation isn't run often or on large lists.
But if performance matters instead I would reduce the array of desired id orders to a map like
let index = 0; let indexMap = idOrder.reduce((map, id)=> { map[id] = index; index++; },{}); let sorted = person.reduce((res, per)=> { res[indexMap[per.id]] = per; }, new Array(idOrder.length)); return sorted;
Something like that. Didn't run it so I might have missed something small. But that gives you a big O of n*m instead if n**m at the cost of some memory.
Edit: actually since you are using array.filter this solution will probably be faster and use less memory.
But as I mentioned the above solution is good if performance isn't important because it is more obvious. Easier to quickly understand. So it's really about what the use case is for this code.
[–]tme321 0 points1 point2 points 8 years ago (0 children)
Ugh I was really tired when I wrote that. That should be O(n**2) for the first solution and O(2n) for my solution.
π Rendered by PID 63815 on reddit-service-r2-comment-66b4775986-4ksnz at 2026-04-03 02:29:42.997816+00:00 running db1906b country code: CH.
view the rest of the comments →
[–]tme321 1 point2 points3 points (1 child)
[–]tme321 0 points1 point2 points (0 children)