How to get the list of versions from an array after the specific version using Node JS Code?
up vote
0
down vote
favorite
I have an array with the list of stacks with versions eg v4.5.3
,
[ '64bit Amazon Linux 2018.03 v4.6.0 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.4 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.3 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.2 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.1 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.0 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.6 running Node.js',
'64bit Amazon Linux v4.4.5 running Node.js',
'64bit Amazon Linux v4.4.4 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.3 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.2 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.1 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.0 running Node.js',
'64bit Amazon Linux 2017.03 v4.3.0 running Node.js',
'64bit Amazon Linux 2017.03 v4.2.2 running Node.js',
'64bit Amazon Linux 2017.03 v4.2.1 running Node.js',
'64bit Amazon Linux 2017.03 v4.2.0 running Node.js']
I want to get the next latest versions after 64bit Amazon Linux 2018.03 v4.5.3 running Node.js
that is
'64bit Amazon Linux 2018.03 v4.6.0 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.4 running Node.js'
Approach 1 : What i did is i looped this array and finded the string "64bit Amazon Linux 2018.03 v4.5.3 running Node.js"
and after that whatever elements where there i pushed to an new array was that the right approach ??
Approach 2 : I can use split of each element and i can find the v4.6.0
but the problem is there is no guratee that these versions will be in the same position.
what is the best approach to do this in Node JS ?
node.js optimization
add a comment |
up vote
0
down vote
favorite
I have an array with the list of stacks with versions eg v4.5.3
,
[ '64bit Amazon Linux 2018.03 v4.6.0 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.4 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.3 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.2 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.1 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.0 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.6 running Node.js',
'64bit Amazon Linux v4.4.5 running Node.js',
'64bit Amazon Linux v4.4.4 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.3 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.2 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.1 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.0 running Node.js',
'64bit Amazon Linux 2017.03 v4.3.0 running Node.js',
'64bit Amazon Linux 2017.03 v4.2.2 running Node.js',
'64bit Amazon Linux 2017.03 v4.2.1 running Node.js',
'64bit Amazon Linux 2017.03 v4.2.0 running Node.js']
I want to get the next latest versions after 64bit Amazon Linux 2018.03 v4.5.3 running Node.js
that is
'64bit Amazon Linux 2018.03 v4.6.0 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.4 running Node.js'
Approach 1 : What i did is i looped this array and finded the string "64bit Amazon Linux 2018.03 v4.5.3 running Node.js"
and after that whatever elements where there i pushed to an new array was that the right approach ??
Approach 2 : I can use split of each element and i can find the v4.6.0
but the problem is there is no guratee that these versions will be in the same position.
what is the best approach to do this in Node JS ?
node.js optimization
is the list already sorted by decending version number?
– pandamakes
Nov 9 at 13:37
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have an array with the list of stacks with versions eg v4.5.3
,
[ '64bit Amazon Linux 2018.03 v4.6.0 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.4 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.3 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.2 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.1 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.0 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.6 running Node.js',
'64bit Amazon Linux v4.4.5 running Node.js',
'64bit Amazon Linux v4.4.4 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.3 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.2 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.1 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.0 running Node.js',
'64bit Amazon Linux 2017.03 v4.3.0 running Node.js',
'64bit Amazon Linux 2017.03 v4.2.2 running Node.js',
'64bit Amazon Linux 2017.03 v4.2.1 running Node.js',
'64bit Amazon Linux 2017.03 v4.2.0 running Node.js']
I want to get the next latest versions after 64bit Amazon Linux 2018.03 v4.5.3 running Node.js
that is
'64bit Amazon Linux 2018.03 v4.6.0 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.4 running Node.js'
Approach 1 : What i did is i looped this array and finded the string "64bit Amazon Linux 2018.03 v4.5.3 running Node.js"
and after that whatever elements where there i pushed to an new array was that the right approach ??
Approach 2 : I can use split of each element and i can find the v4.6.0
but the problem is there is no guratee that these versions will be in the same position.
what is the best approach to do this in Node JS ?
node.js optimization
I have an array with the list of stacks with versions eg v4.5.3
,
[ '64bit Amazon Linux 2018.03 v4.6.0 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.4 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.3 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.2 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.1 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.0 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.6 running Node.js',
'64bit Amazon Linux v4.4.5 running Node.js',
'64bit Amazon Linux v4.4.4 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.3 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.2 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.1 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.0 running Node.js',
'64bit Amazon Linux 2017.03 v4.3.0 running Node.js',
'64bit Amazon Linux 2017.03 v4.2.2 running Node.js',
'64bit Amazon Linux 2017.03 v4.2.1 running Node.js',
'64bit Amazon Linux 2017.03 v4.2.0 running Node.js']
I want to get the next latest versions after 64bit Amazon Linux 2018.03 v4.5.3 running Node.js
that is
'64bit Amazon Linux 2018.03 v4.6.0 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.4 running Node.js'
Approach 1 : What i did is i looped this array and finded the string "64bit Amazon Linux 2018.03 v4.5.3 running Node.js"
and after that whatever elements where there i pushed to an new array was that the right approach ??
Approach 2 : I can use split of each element and i can find the v4.6.0
but the problem is there is no guratee that these versions will be in the same position.
what is the best approach to do this in Node JS ?
node.js optimization
node.js optimization
asked Nov 9 at 13:10
Vishnu Ranganathan
602524
602524
is the list already sorted by decending version number?
– pandamakes
Nov 9 at 13:37
add a comment |
is the list already sorted by decending version number?
– pandamakes
Nov 9 at 13:37
is the list already sorted by decending version number?
– pandamakes
Nov 9 at 13:37
is the list already sorted by decending version number?
– pandamakes
Nov 9 at 13:37
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
First you can use a regex to get the version.
const myVersions = versions.map((str) => {
let match = str.match(/vd+.d+.d+/)[0];
let temp = match.split('');
temp.shift();
match = temp.join('');
return {
semver: match,
text: str
};
});
This will return versions as an array of objects with fields semever like 4.0.2
and text like '64bit Amazon Linux 2018.03 v4.0.2 running Node.js'
.
Secondly you can write a compare function to sort according to the version numbers and get the desired output or you can use the compare-versions
library for sorting which is already specified in the other answer.
function myCompareFunc(v1, v2) {
let [major1, minor1, patch1] = v1.semver.split('.').map(Number);
let [major2, minor2, patch2] = v2.semver.split('.').map(Number);
if (major1 > major2) {
return -1;
}
else if (major2 > major1) {
return 1;
}
else if (minor1 > minor2) {
return -1;
}
else if (minor2 > minor1) {
return 1;
}
else if (patch1 > patch2) {
return -1;
}
else if (patch2 > patch1) {
return 1;
}
else {
return 0;
}
}
myVersions.sort(myCompareFunc);
console.log(myVersions.map(ver=>ver.text));
DEMO
const versions = ['64bit Amazon Linux 2018.03 v4.6.0 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.4 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.3 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.6 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.2 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.0 running Node.js',
'64bit Amazon Linux v4.4.5 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.3 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.2 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.1 running Node.js',
'64bit Amazon Linux v4.4.4 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.0 running Node.js',
'64bit Amazon Linux 2017.03 v4.3.0 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.1 running Node.js',
'64bit Amazon Linux 2017.03 v4.2.2 running Node.js',
'64bit Amazon Linux 2017.03 v4.2.1 running Node.js',
'64bit Amazon Linux 2017.03 v4.2.0 running Node.js'];
const myVersions = versions.map((str) => {
let match = str.match(/vd+.d+.d+/)[0];
let temp = match.split('');
temp.shift();
match = temp.join('');
return {
semver: match,
text: str
};
});
function myCompareFunc(v1, v2) {
let [major1, minor1, patch1] = v1.semver.split('.').map(Number);
let [major2, minor2, patch2] = v2.semver.split('.').map(Number);
if (major1 > major2) {
return -1;
}
else if (major2 > major1) {
return 1;
}
else if (minor1 > minor2) {
return -1;
}
else if (minor2 > minor1) {
return 1;
}
else if (patch1 > patch2) {
return -1;
}
else if (patch2 > patch1) {
return 1;
}
else {
return 0;
}
}
myVersions.sort(myCompareFunc);
console.log(myVersions.map(ver=>ver.text));
add a comment |
up vote
1
down vote
As you said in "Approach 2", I'm supposing you already obtained an array containing only the version numbers. You could use compare-versions.
This library is able to:
Compare semver version strings to find greater, equal or lesser.
var compareVersions = require('compare-versions');
var versions = [
'1.5.19',
'1.2.3',
'1.5.5'
]
var sorted = versions.sort(compareVersions);
/*
[
'1.2.3',
'1.5.5',
'1.5.19'
]
*/
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',
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%2f53226334%2fhow-to-get-the-list-of-versions-from-an-array-after-the-specific-version-using-n%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
up vote
1
down vote
accepted
First you can use a regex to get the version.
const myVersions = versions.map((str) => {
let match = str.match(/vd+.d+.d+/)[0];
let temp = match.split('');
temp.shift();
match = temp.join('');
return {
semver: match,
text: str
};
});
This will return versions as an array of objects with fields semever like 4.0.2
and text like '64bit Amazon Linux 2018.03 v4.0.2 running Node.js'
.
Secondly you can write a compare function to sort according to the version numbers and get the desired output or you can use the compare-versions
library for sorting which is already specified in the other answer.
function myCompareFunc(v1, v2) {
let [major1, minor1, patch1] = v1.semver.split('.').map(Number);
let [major2, minor2, patch2] = v2.semver.split('.').map(Number);
if (major1 > major2) {
return -1;
}
else if (major2 > major1) {
return 1;
}
else if (minor1 > minor2) {
return -1;
}
else if (minor2 > minor1) {
return 1;
}
else if (patch1 > patch2) {
return -1;
}
else if (patch2 > patch1) {
return 1;
}
else {
return 0;
}
}
myVersions.sort(myCompareFunc);
console.log(myVersions.map(ver=>ver.text));
DEMO
const versions = ['64bit Amazon Linux 2018.03 v4.6.0 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.4 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.3 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.6 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.2 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.0 running Node.js',
'64bit Amazon Linux v4.4.5 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.3 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.2 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.1 running Node.js',
'64bit Amazon Linux v4.4.4 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.0 running Node.js',
'64bit Amazon Linux 2017.03 v4.3.0 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.1 running Node.js',
'64bit Amazon Linux 2017.03 v4.2.2 running Node.js',
'64bit Amazon Linux 2017.03 v4.2.1 running Node.js',
'64bit Amazon Linux 2017.03 v4.2.0 running Node.js'];
const myVersions = versions.map((str) => {
let match = str.match(/vd+.d+.d+/)[0];
let temp = match.split('');
temp.shift();
match = temp.join('');
return {
semver: match,
text: str
};
});
function myCompareFunc(v1, v2) {
let [major1, minor1, patch1] = v1.semver.split('.').map(Number);
let [major2, minor2, patch2] = v2.semver.split('.').map(Number);
if (major1 > major2) {
return -1;
}
else if (major2 > major1) {
return 1;
}
else if (minor1 > minor2) {
return -1;
}
else if (minor2 > minor1) {
return 1;
}
else if (patch1 > patch2) {
return -1;
}
else if (patch2 > patch1) {
return 1;
}
else {
return 0;
}
}
myVersions.sort(myCompareFunc);
console.log(myVersions.map(ver=>ver.text));
add a comment |
up vote
1
down vote
accepted
First you can use a regex to get the version.
const myVersions = versions.map((str) => {
let match = str.match(/vd+.d+.d+/)[0];
let temp = match.split('');
temp.shift();
match = temp.join('');
return {
semver: match,
text: str
};
});
This will return versions as an array of objects with fields semever like 4.0.2
and text like '64bit Amazon Linux 2018.03 v4.0.2 running Node.js'
.
Secondly you can write a compare function to sort according to the version numbers and get the desired output or you can use the compare-versions
library for sorting which is already specified in the other answer.
function myCompareFunc(v1, v2) {
let [major1, minor1, patch1] = v1.semver.split('.').map(Number);
let [major2, minor2, patch2] = v2.semver.split('.').map(Number);
if (major1 > major2) {
return -1;
}
else if (major2 > major1) {
return 1;
}
else if (minor1 > minor2) {
return -1;
}
else if (minor2 > minor1) {
return 1;
}
else if (patch1 > patch2) {
return -1;
}
else if (patch2 > patch1) {
return 1;
}
else {
return 0;
}
}
myVersions.sort(myCompareFunc);
console.log(myVersions.map(ver=>ver.text));
DEMO
const versions = ['64bit Amazon Linux 2018.03 v4.6.0 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.4 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.3 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.6 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.2 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.0 running Node.js',
'64bit Amazon Linux v4.4.5 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.3 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.2 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.1 running Node.js',
'64bit Amazon Linux v4.4.4 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.0 running Node.js',
'64bit Amazon Linux 2017.03 v4.3.0 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.1 running Node.js',
'64bit Amazon Linux 2017.03 v4.2.2 running Node.js',
'64bit Amazon Linux 2017.03 v4.2.1 running Node.js',
'64bit Amazon Linux 2017.03 v4.2.0 running Node.js'];
const myVersions = versions.map((str) => {
let match = str.match(/vd+.d+.d+/)[0];
let temp = match.split('');
temp.shift();
match = temp.join('');
return {
semver: match,
text: str
};
});
function myCompareFunc(v1, v2) {
let [major1, minor1, patch1] = v1.semver.split('.').map(Number);
let [major2, minor2, patch2] = v2.semver.split('.').map(Number);
if (major1 > major2) {
return -1;
}
else if (major2 > major1) {
return 1;
}
else if (minor1 > minor2) {
return -1;
}
else if (minor2 > minor1) {
return 1;
}
else if (patch1 > patch2) {
return -1;
}
else if (patch2 > patch1) {
return 1;
}
else {
return 0;
}
}
myVersions.sort(myCompareFunc);
console.log(myVersions.map(ver=>ver.text));
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
First you can use a regex to get the version.
const myVersions = versions.map((str) => {
let match = str.match(/vd+.d+.d+/)[0];
let temp = match.split('');
temp.shift();
match = temp.join('');
return {
semver: match,
text: str
};
});
This will return versions as an array of objects with fields semever like 4.0.2
and text like '64bit Amazon Linux 2018.03 v4.0.2 running Node.js'
.
Secondly you can write a compare function to sort according to the version numbers and get the desired output or you can use the compare-versions
library for sorting which is already specified in the other answer.
function myCompareFunc(v1, v2) {
let [major1, minor1, patch1] = v1.semver.split('.').map(Number);
let [major2, minor2, patch2] = v2.semver.split('.').map(Number);
if (major1 > major2) {
return -1;
}
else if (major2 > major1) {
return 1;
}
else if (minor1 > minor2) {
return -1;
}
else if (minor2 > minor1) {
return 1;
}
else if (patch1 > patch2) {
return -1;
}
else if (patch2 > patch1) {
return 1;
}
else {
return 0;
}
}
myVersions.sort(myCompareFunc);
console.log(myVersions.map(ver=>ver.text));
DEMO
const versions = ['64bit Amazon Linux 2018.03 v4.6.0 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.4 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.3 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.6 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.2 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.0 running Node.js',
'64bit Amazon Linux v4.4.5 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.3 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.2 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.1 running Node.js',
'64bit Amazon Linux v4.4.4 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.0 running Node.js',
'64bit Amazon Linux 2017.03 v4.3.0 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.1 running Node.js',
'64bit Amazon Linux 2017.03 v4.2.2 running Node.js',
'64bit Amazon Linux 2017.03 v4.2.1 running Node.js',
'64bit Amazon Linux 2017.03 v4.2.0 running Node.js'];
const myVersions = versions.map((str) => {
let match = str.match(/vd+.d+.d+/)[0];
let temp = match.split('');
temp.shift();
match = temp.join('');
return {
semver: match,
text: str
};
});
function myCompareFunc(v1, v2) {
let [major1, minor1, patch1] = v1.semver.split('.').map(Number);
let [major2, minor2, patch2] = v2.semver.split('.').map(Number);
if (major1 > major2) {
return -1;
}
else if (major2 > major1) {
return 1;
}
else if (minor1 > minor2) {
return -1;
}
else if (minor2 > minor1) {
return 1;
}
else if (patch1 > patch2) {
return -1;
}
else if (patch2 > patch1) {
return 1;
}
else {
return 0;
}
}
myVersions.sort(myCompareFunc);
console.log(myVersions.map(ver=>ver.text));
First you can use a regex to get the version.
const myVersions = versions.map((str) => {
let match = str.match(/vd+.d+.d+/)[0];
let temp = match.split('');
temp.shift();
match = temp.join('');
return {
semver: match,
text: str
};
});
This will return versions as an array of objects with fields semever like 4.0.2
and text like '64bit Amazon Linux 2018.03 v4.0.2 running Node.js'
.
Secondly you can write a compare function to sort according to the version numbers and get the desired output or you can use the compare-versions
library for sorting which is already specified in the other answer.
function myCompareFunc(v1, v2) {
let [major1, minor1, patch1] = v1.semver.split('.').map(Number);
let [major2, minor2, patch2] = v2.semver.split('.').map(Number);
if (major1 > major2) {
return -1;
}
else if (major2 > major1) {
return 1;
}
else if (minor1 > minor2) {
return -1;
}
else if (minor2 > minor1) {
return 1;
}
else if (patch1 > patch2) {
return -1;
}
else if (patch2 > patch1) {
return 1;
}
else {
return 0;
}
}
myVersions.sort(myCompareFunc);
console.log(myVersions.map(ver=>ver.text));
DEMO
const versions = ['64bit Amazon Linux 2018.03 v4.6.0 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.4 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.3 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.6 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.2 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.0 running Node.js',
'64bit Amazon Linux v4.4.5 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.3 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.2 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.1 running Node.js',
'64bit Amazon Linux v4.4.4 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.0 running Node.js',
'64bit Amazon Linux 2017.03 v4.3.0 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.1 running Node.js',
'64bit Amazon Linux 2017.03 v4.2.2 running Node.js',
'64bit Amazon Linux 2017.03 v4.2.1 running Node.js',
'64bit Amazon Linux 2017.03 v4.2.0 running Node.js'];
const myVersions = versions.map((str) => {
let match = str.match(/vd+.d+.d+/)[0];
let temp = match.split('');
temp.shift();
match = temp.join('');
return {
semver: match,
text: str
};
});
function myCompareFunc(v1, v2) {
let [major1, minor1, patch1] = v1.semver.split('.').map(Number);
let [major2, minor2, patch2] = v2.semver.split('.').map(Number);
if (major1 > major2) {
return -1;
}
else if (major2 > major1) {
return 1;
}
else if (minor1 > minor2) {
return -1;
}
else if (minor2 > minor1) {
return 1;
}
else if (patch1 > patch2) {
return -1;
}
else if (patch2 > patch1) {
return 1;
}
else {
return 0;
}
}
myVersions.sort(myCompareFunc);
console.log(myVersions.map(ver=>ver.text));
const versions = ['64bit Amazon Linux 2018.03 v4.6.0 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.4 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.3 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.6 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.2 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.0 running Node.js',
'64bit Amazon Linux v4.4.5 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.3 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.2 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.1 running Node.js',
'64bit Amazon Linux v4.4.4 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.0 running Node.js',
'64bit Amazon Linux 2017.03 v4.3.0 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.1 running Node.js',
'64bit Amazon Linux 2017.03 v4.2.2 running Node.js',
'64bit Amazon Linux 2017.03 v4.2.1 running Node.js',
'64bit Amazon Linux 2017.03 v4.2.0 running Node.js'];
const myVersions = versions.map((str) => {
let match = str.match(/vd+.d+.d+/)[0];
let temp = match.split('');
temp.shift();
match = temp.join('');
return {
semver: match,
text: str
};
});
function myCompareFunc(v1, v2) {
let [major1, minor1, patch1] = v1.semver.split('.').map(Number);
let [major2, minor2, patch2] = v2.semver.split('.').map(Number);
if (major1 > major2) {
return -1;
}
else if (major2 > major1) {
return 1;
}
else if (minor1 > minor2) {
return -1;
}
else if (minor2 > minor1) {
return 1;
}
else if (patch1 > patch2) {
return -1;
}
else if (patch2 > patch1) {
return 1;
}
else {
return 0;
}
}
myVersions.sort(myCompareFunc);
console.log(myVersions.map(ver=>ver.text));
const versions = ['64bit Amazon Linux 2018.03 v4.6.0 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.4 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.3 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.6 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.2 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.0 running Node.js',
'64bit Amazon Linux v4.4.5 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.3 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.2 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.1 running Node.js',
'64bit Amazon Linux v4.4.4 running Node.js',
'64bit Amazon Linux 2017.09 v4.4.0 running Node.js',
'64bit Amazon Linux 2017.03 v4.3.0 running Node.js',
'64bit Amazon Linux 2018.03 v4.5.1 running Node.js',
'64bit Amazon Linux 2017.03 v4.2.2 running Node.js',
'64bit Amazon Linux 2017.03 v4.2.1 running Node.js',
'64bit Amazon Linux 2017.03 v4.2.0 running Node.js'];
const myVersions = versions.map((str) => {
let match = str.match(/vd+.d+.d+/)[0];
let temp = match.split('');
temp.shift();
match = temp.join('');
return {
semver: match,
text: str
};
});
function myCompareFunc(v1, v2) {
let [major1, minor1, patch1] = v1.semver.split('.').map(Number);
let [major2, minor2, patch2] = v2.semver.split('.').map(Number);
if (major1 > major2) {
return -1;
}
else if (major2 > major1) {
return 1;
}
else if (minor1 > minor2) {
return -1;
}
else if (minor2 > minor1) {
return 1;
}
else if (patch1 > patch2) {
return -1;
}
else if (patch2 > patch1) {
return 1;
}
else {
return 0;
}
}
myVersions.sort(myCompareFunc);
console.log(myVersions.map(ver=>ver.text));
answered Nov 9 at 14:03
vibhor1997a
1,5591624
1,5591624
add a comment |
add a comment |
up vote
1
down vote
As you said in "Approach 2", I'm supposing you already obtained an array containing only the version numbers. You could use compare-versions.
This library is able to:
Compare semver version strings to find greater, equal or lesser.
var compareVersions = require('compare-versions');
var versions = [
'1.5.19',
'1.2.3',
'1.5.5'
]
var sorted = versions.sort(compareVersions);
/*
[
'1.2.3',
'1.5.5',
'1.5.19'
]
*/
add a comment |
up vote
1
down vote
As you said in "Approach 2", I'm supposing you already obtained an array containing only the version numbers. You could use compare-versions.
This library is able to:
Compare semver version strings to find greater, equal or lesser.
var compareVersions = require('compare-versions');
var versions = [
'1.5.19',
'1.2.3',
'1.5.5'
]
var sorted = versions.sort(compareVersions);
/*
[
'1.2.3',
'1.5.5',
'1.5.19'
]
*/
add a comment |
up vote
1
down vote
up vote
1
down vote
As you said in "Approach 2", I'm supposing you already obtained an array containing only the version numbers. You could use compare-versions.
This library is able to:
Compare semver version strings to find greater, equal or lesser.
var compareVersions = require('compare-versions');
var versions = [
'1.5.19',
'1.2.3',
'1.5.5'
]
var sorted = versions.sort(compareVersions);
/*
[
'1.2.3',
'1.5.5',
'1.5.19'
]
*/
As you said in "Approach 2", I'm supposing you already obtained an array containing only the version numbers. You could use compare-versions.
This library is able to:
Compare semver version strings to find greater, equal or lesser.
var compareVersions = require('compare-versions');
var versions = [
'1.5.19',
'1.2.3',
'1.5.5'
]
var sorted = versions.sort(compareVersions);
/*
[
'1.2.3',
'1.5.5',
'1.5.19'
]
*/
answered Nov 9 at 13:26
Radar155
490216
490216
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53226334%2fhow-to-get-the-list-of-versions-from-an-array-after-the-specific-version-using-n%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
is the list already sorted by decending version number?
– pandamakes
Nov 9 at 13:37