/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Sorting Array based on substring with predefined priority

Hey All,

I’m trying to sort / prioritize an array with substrings. I only care about whats after the “@” symbol… the domain. The array will always be of different sizes, order and may or may not contain of the domains I wan’t prioritized. See below as example

[‘usr1@[B]domx.com[/B]‘,’[email protected]‘,’usr1@[B]domy.com[/B]‘,’[email protected]‘,’usr1@[B]domz.com[/B]‘]

So what if i want to it to look would be like below, always with x then y then z. z always last if it exists

[‘[email protected]‘,’[email protected]‘,’usr1@[B]domx.com[/B]‘,’usr1@[B]domy.com[/B]‘,’usr1@[B]domz.com[/B]‘]

there may instances when x/y/z domains don’t exist. I don’t care about the order of any other domains

I’ve tried sort() with a compare function and i can always get domz.com as last element but can’t control anything after that

Appreciate some help

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@KeverJun 16.2016 — Does this work correct?function compare(a, b) {
a = a.split('@')[1];
b = b.split('@')[1];
var factors = ['domx.com', 'domy.com', 'domz.com'];
return factors.indexOf(a) - factors.indexOf(b);
};
Copy linkTweet thisAlerts:
@geopginauthorJun 16.2016 — Hi Kever,

The works perfectly, havI can't seem to wrap my head around why though. Thanks so much Kever

Off to ponder
×

Success!

Help @geopgin 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 5.25,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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