Angular: sorting software versions alphabetically
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I need to alphabetize a list of software versions in Angular from latest to oldest (i.e. 1.10.3.e, 1.9.5.b, 1.7.3, 1.5.1.c). I have been using .sort() which sorts alphabetically but treats the values as a string resulting in any 1.10 version to be sorted after a 1.5 version as it looks at the 1 and 0 independently.I tried doing .sort((a, b) => b - a) and it completely randomized the list rather than sorting it. I have also tried using .slice('.') which does not seem to work.
Any ideas?
sorting version alphabetical-sort
add a comment |
I need to alphabetize a list of software versions in Angular from latest to oldest (i.e. 1.10.3.e, 1.9.5.b, 1.7.3, 1.5.1.c). I have been using .sort() which sorts alphabetically but treats the values as a string resulting in any 1.10 version to be sorted after a 1.5 version as it looks at the 1 and 0 independently.I tried doing .sort((a, b) => b - a) and it completely randomized the list rather than sorting it. I have also tried using .slice('.') which does not seem to work.
Any ideas?
sorting version alphabetical-sort
This is not really an angular question, any typescript/javascript solution will work. You can check this question: stackoverflow.com/questions/6832596/…
– LambdaCruiser
Jan 3 at 14:54
add a comment |
I need to alphabetize a list of software versions in Angular from latest to oldest (i.e. 1.10.3.e, 1.9.5.b, 1.7.3, 1.5.1.c). I have been using .sort() which sorts alphabetically but treats the values as a string resulting in any 1.10 version to be sorted after a 1.5 version as it looks at the 1 and 0 independently.I tried doing .sort((a, b) => b - a) and it completely randomized the list rather than sorting it. I have also tried using .slice('.') which does not seem to work.
Any ideas?
sorting version alphabetical-sort
I need to alphabetize a list of software versions in Angular from latest to oldest (i.e. 1.10.3.e, 1.9.5.b, 1.7.3, 1.5.1.c). I have been using .sort() which sorts alphabetically but treats the values as a string resulting in any 1.10 version to be sorted after a 1.5 version as it looks at the 1 and 0 independently.I tried doing .sort((a, b) => b - a) and it completely randomized the list rather than sorting it. I have also tried using .slice('.') which does not seem to work.
Any ideas?
sorting version alphabetical-sort
sorting version alphabetical-sort
edited Jan 3 at 16:25
peinearydevelopment
5,06642548
5,06642548
asked Jan 3 at 13:42
Cole HendersonCole Henderson
155
155
This is not really an angular question, any typescript/javascript solution will work. You can check this question: stackoverflow.com/questions/6832596/…
– LambdaCruiser
Jan 3 at 14:54
add a comment |
This is not really an angular question, any typescript/javascript solution will work. You can check this question: stackoverflow.com/questions/6832596/…
– LambdaCruiser
Jan 3 at 14:54
This is not really an angular question, any typescript/javascript solution will work. You can check this question: stackoverflow.com/questions/6832596/…
– LambdaCruiser
Jan 3 at 14:54
This is not really an angular question, any typescript/javascript solution will work. You can check this question: stackoverflow.com/questions/6832596/…
– LambdaCruiser
Jan 3 at 14:54
add a comment |
2 Answers
2
active
oldest
votes
Solution 1): temporary pad with '0':
(If a part of the version number could become more than 2 digits, padStart could be changed to 3, 4, ...)
var vers = [ '1.10.3.e', '1.9.5.b', '1.7.3', '1.5.1.c' ];
console.log( vers );
var temp = vers.map( function( s ){
return s.split('.').map( function( n ){ return n.padStart(2,'0'); } ).join('.');
} )
temp.sort();
vers = temp.map( function( s ){
return s.split('.').map( function( n ){ return n.replace(/^0+/, ''); } ).join('.');
} )
console.log( temp );
console.log( vers );
Solution 2): sort seperately by integers and strings.
I would call this the more elegant solution, even if it is a bit more complicated.
(Such an answer, with integers, has been given already)
Worked like a charm. Some of my versions have the number 0 at the end so I replaced .padStart(2, '0') with .padStart(2. '!'). Thank you!
– Cole Henderson
Jan 3 at 17:19
add a comment |
This isn't an easy question to answer as there are a few variables to consider. Assuming that you will never have versions like 1.1, 1.a, then the following should do the trick.
var arr = ['1.10.3.e', '1.9.5.b', '1.7.3', '1.5.1.c', '1.5.1.a', '1.5.1.d', '1.10', '1.5']
.map(a => a.split('.') // split by decimals
.map(i => Number.isNaN(parseInt(i, 10)) ? i : parseInt(i, 10))) // make numbers numbers, leave chars as strings
.sort((a,b) => {
// sort by multiple indexes and if one array is shorter, it's i item will be undefined
var longerIndex = a.length > b.length ? a.length : b.length;
for(var i = 0, l = longerIndex; i < l; i++) {
if (a[i] > b[i] || b[i] === undefined) return 1;
else if (a[i] < b[i] || a[i] === undefined) return -1;
}
return 0;
})
.map(a => a.join('.')) // join the version parts back into a single string
console.log(arr);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54023482%2fangular-sorting-software-versions-alphabetically%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Solution 1): temporary pad with '0':
(If a part of the version number could become more than 2 digits, padStart could be changed to 3, 4, ...)
var vers = [ '1.10.3.e', '1.9.5.b', '1.7.3', '1.5.1.c' ];
console.log( vers );
var temp = vers.map( function( s ){
return s.split('.').map( function( n ){ return n.padStart(2,'0'); } ).join('.');
} )
temp.sort();
vers = temp.map( function( s ){
return s.split('.').map( function( n ){ return n.replace(/^0+/, ''); } ).join('.');
} )
console.log( temp );
console.log( vers );
Solution 2): sort seperately by integers and strings.
I would call this the more elegant solution, even if it is a bit more complicated.
(Such an answer, with integers, has been given already)
Worked like a charm. Some of my versions have the number 0 at the end so I replaced .padStart(2, '0') with .padStart(2. '!'). Thank you!
– Cole Henderson
Jan 3 at 17:19
add a comment |
Solution 1): temporary pad with '0':
(If a part of the version number could become more than 2 digits, padStart could be changed to 3, 4, ...)
var vers = [ '1.10.3.e', '1.9.5.b', '1.7.3', '1.5.1.c' ];
console.log( vers );
var temp = vers.map( function( s ){
return s.split('.').map( function( n ){ return n.padStart(2,'0'); } ).join('.');
} )
temp.sort();
vers = temp.map( function( s ){
return s.split('.').map( function( n ){ return n.replace(/^0+/, ''); } ).join('.');
} )
console.log( temp );
console.log( vers );
Solution 2): sort seperately by integers and strings.
I would call this the more elegant solution, even if it is a bit more complicated.
(Such an answer, with integers, has been given already)
Worked like a charm. Some of my versions have the number 0 at the end so I replaced .padStart(2, '0') with .padStart(2. '!'). Thank you!
– Cole Henderson
Jan 3 at 17:19
add a comment |
Solution 1): temporary pad with '0':
(If a part of the version number could become more than 2 digits, padStart could be changed to 3, 4, ...)
var vers = [ '1.10.3.e', '1.9.5.b', '1.7.3', '1.5.1.c' ];
console.log( vers );
var temp = vers.map( function( s ){
return s.split('.').map( function( n ){ return n.padStart(2,'0'); } ).join('.');
} )
temp.sort();
vers = temp.map( function( s ){
return s.split('.').map( function( n ){ return n.replace(/^0+/, ''); } ).join('.');
} )
console.log( temp );
console.log( vers );
Solution 2): sort seperately by integers and strings.
I would call this the more elegant solution, even if it is a bit more complicated.
(Such an answer, with integers, has been given already)
Solution 1): temporary pad with '0':
(If a part of the version number could become more than 2 digits, padStart could be changed to 3, 4, ...)
var vers = [ '1.10.3.e', '1.9.5.b', '1.7.3', '1.5.1.c' ];
console.log( vers );
var temp = vers.map( function( s ){
return s.split('.').map( function( n ){ return n.padStart(2,'0'); } ).join('.');
} )
temp.sort();
vers = temp.map( function( s ){
return s.split('.').map( function( n ){ return n.replace(/^0+/, ''); } ).join('.');
} )
console.log( temp );
console.log( vers );
Solution 2): sort seperately by integers and strings.
I would call this the more elegant solution, even if it is a bit more complicated.
(Such an answer, with integers, has been given already)
edited Jan 3 at 14:24
answered Jan 3 at 14:15
kiciakicia
1214
1214
Worked like a charm. Some of my versions have the number 0 at the end so I replaced .padStart(2, '0') with .padStart(2. '!'). Thank you!
– Cole Henderson
Jan 3 at 17:19
add a comment |
Worked like a charm. Some of my versions have the number 0 at the end so I replaced .padStart(2, '0') with .padStart(2. '!'). Thank you!
– Cole Henderson
Jan 3 at 17:19
Worked like a charm. Some of my versions have the number 0 at the end so I replaced .padStart(2, '0') with .padStart(2. '!'). Thank you!
– Cole Henderson
Jan 3 at 17:19
Worked like a charm. Some of my versions have the number 0 at the end so I replaced .padStart(2, '0') with .padStart(2. '!'). Thank you!
– Cole Henderson
Jan 3 at 17:19
add a comment |
This isn't an easy question to answer as there are a few variables to consider. Assuming that you will never have versions like 1.1, 1.a, then the following should do the trick.
var arr = ['1.10.3.e', '1.9.5.b', '1.7.3', '1.5.1.c', '1.5.1.a', '1.5.1.d', '1.10', '1.5']
.map(a => a.split('.') // split by decimals
.map(i => Number.isNaN(parseInt(i, 10)) ? i : parseInt(i, 10))) // make numbers numbers, leave chars as strings
.sort((a,b) => {
// sort by multiple indexes and if one array is shorter, it's i item will be undefined
var longerIndex = a.length > b.length ? a.length : b.length;
for(var i = 0, l = longerIndex; i < l; i++) {
if (a[i] > b[i] || b[i] === undefined) return 1;
else if (a[i] < b[i] || a[i] === undefined) return -1;
}
return 0;
})
.map(a => a.join('.')) // join the version parts back into a single string
console.log(arr);
add a comment |
This isn't an easy question to answer as there are a few variables to consider. Assuming that you will never have versions like 1.1, 1.a, then the following should do the trick.
var arr = ['1.10.3.e', '1.9.5.b', '1.7.3', '1.5.1.c', '1.5.1.a', '1.5.1.d', '1.10', '1.5']
.map(a => a.split('.') // split by decimals
.map(i => Number.isNaN(parseInt(i, 10)) ? i : parseInt(i, 10))) // make numbers numbers, leave chars as strings
.sort((a,b) => {
// sort by multiple indexes and if one array is shorter, it's i item will be undefined
var longerIndex = a.length > b.length ? a.length : b.length;
for(var i = 0, l = longerIndex; i < l; i++) {
if (a[i] > b[i] || b[i] === undefined) return 1;
else if (a[i] < b[i] || a[i] === undefined) return -1;
}
return 0;
})
.map(a => a.join('.')) // join the version parts back into a single string
console.log(arr);
add a comment |
This isn't an easy question to answer as there are a few variables to consider. Assuming that you will never have versions like 1.1, 1.a, then the following should do the trick.
var arr = ['1.10.3.e', '1.9.5.b', '1.7.3', '1.5.1.c', '1.5.1.a', '1.5.1.d', '1.10', '1.5']
.map(a => a.split('.') // split by decimals
.map(i => Number.isNaN(parseInt(i, 10)) ? i : parseInt(i, 10))) // make numbers numbers, leave chars as strings
.sort((a,b) => {
// sort by multiple indexes and if one array is shorter, it's i item will be undefined
var longerIndex = a.length > b.length ? a.length : b.length;
for(var i = 0, l = longerIndex; i < l; i++) {
if (a[i] > b[i] || b[i] === undefined) return 1;
else if (a[i] < b[i] || a[i] === undefined) return -1;
}
return 0;
})
.map(a => a.join('.')) // join the version parts back into a single string
console.log(arr);
This isn't an easy question to answer as there are a few variables to consider. Assuming that you will never have versions like 1.1, 1.a, then the following should do the trick.
var arr = ['1.10.3.e', '1.9.5.b', '1.7.3', '1.5.1.c', '1.5.1.a', '1.5.1.d', '1.10', '1.5']
.map(a => a.split('.') // split by decimals
.map(i => Number.isNaN(parseInt(i, 10)) ? i : parseInt(i, 10))) // make numbers numbers, leave chars as strings
.sort((a,b) => {
// sort by multiple indexes and if one array is shorter, it's i item will be undefined
var longerIndex = a.length > b.length ? a.length : b.length;
for(var i = 0, l = longerIndex; i < l; i++) {
if (a[i] > b[i] || b[i] === undefined) return 1;
else if (a[i] < b[i] || a[i] === undefined) return -1;
}
return 0;
})
.map(a => a.join('.')) // join the version parts back into a single string
console.log(arr);
var arr = ['1.10.3.e', '1.9.5.b', '1.7.3', '1.5.1.c', '1.5.1.a', '1.5.1.d', '1.10', '1.5']
.map(a => a.split('.') // split by decimals
.map(i => Number.isNaN(parseInt(i, 10)) ? i : parseInt(i, 10))) // make numbers numbers, leave chars as strings
.sort((a,b) => {
// sort by multiple indexes and if one array is shorter, it's i item will be undefined
var longerIndex = a.length > b.length ? a.length : b.length;
for(var i = 0, l = longerIndex; i < l; i++) {
if (a[i] > b[i] || b[i] === undefined) return 1;
else if (a[i] < b[i] || a[i] === undefined) return -1;
}
return 0;
})
.map(a => a.join('.')) // join the version parts back into a single string
console.log(arr);
var arr = ['1.10.3.e', '1.9.5.b', '1.7.3', '1.5.1.c', '1.5.1.a', '1.5.1.d', '1.10', '1.5']
.map(a => a.split('.') // split by decimals
.map(i => Number.isNaN(parseInt(i, 10)) ? i : parseInt(i, 10))) // make numbers numbers, leave chars as strings
.sort((a,b) => {
// sort by multiple indexes and if one array is shorter, it's i item will be undefined
var longerIndex = a.length > b.length ? a.length : b.length;
for(var i = 0, l = longerIndex; i < l; i++) {
if (a[i] > b[i] || b[i] === undefined) return 1;
else if (a[i] < b[i] || a[i] === undefined) return -1;
}
return 0;
})
.map(a => a.join('.')) // join the version parts back into a single string
console.log(arr);
answered Jan 3 at 14:06
peinearydevelopmentpeinearydevelopment
5,06642548
5,06642548
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54023482%2fangular-sorting-software-versions-alphabetically%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
This is not really an angular question, any typescript/javascript solution will work. You can check this question: stackoverflow.com/questions/6832596/…
– LambdaCruiser
Jan 3 at 14:54