Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3ad6601

Browse files
Create sortAccounts.js
1 parent d9edf75 commit 3ad6601

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

‎Javascript/sortAccounts.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*Given an array of account objects. Account object contains two keys
2+
3+
created
4+
5+
accountHolderName
6+
7+
8+
Sort the array in the following way -
9+
10+
All the elements should be sorted according to the created field such that the most recently created account comes first.
11+
12+
If two accounts are created on the same date then they should be ordered in ascending order of account holders name.
13+
*/
14+
15+
function sortAccounts(arr) {
16+
return arr.sort((a, b)=>{
17+
if(a.created > b.created){
18+
return -1
19+
}else if(b.created > a.created){
20+
return 1
21+
}else {
22+
if(a.accountHolderName > b.accountHolderName)
23+
return 1
24+
else return -1
25+
}
26+
return arr
27+
})
28+
29+
}
30+
31+
32+
const arr = [
33+
34+
{
35+
36+
created: new Date("05/22/1859"),
37+
38+
accountHolderName: "Arthur"
39+
40+
},
41+
42+
{
43+
44+
created: new Date("03/25/2001"),
45+
46+
accountHolderName: "Doyle"
47+
48+
},
49+
50+
{
51+
52+
created: new Date("03/25/2001"),
53+
54+
accountHolderName: "Conan"
55+
56+
}
57+
58+
]
59+
60+
console.log(sortAccounts(arr))
61+
module.exports = sortAccounts;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /