How to sort strings with number in Javascript

Issue #251

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function sort() {
const string =
`
- Favorite WWDC 2017 sessions https://github.com/onmyway133/blog/issues/56
- Favorite WWDC 2018 sessions https://github.com/onmyway133/blog/issues/245
- How to do clustering with Google Maps in iOS https://github.com/onmyway133/blog/issues/191
`

const lines = string
.split('\n')
.filter((line) => { return line.length > 0 })
.map((line) => {
let parts = line.trimEnd().split(' ')
let lastPart = parts[parts.length-1]
let number = lastPart.replace('https://github.com/onmyway133/blog/issues/', '')
return {
line,
number: parseInt(number)
}
})

lines.sort((a, b) => {
return (a.number < b.number) ? -1 : 1
})

const sortedString = lines
.map((tuple) => {
return tuple.line
})
.join('\n')

console.log(sortedString)
}

Then node index.js

Comments