admin管理员组文章数量:1327685
Problem in Javascript
I previously asked this question Javascript 2D array sorting - by numerical value with a solution provided using the sort function, which I have tried to use without sucsess. Turns out the sort function isn't correctly within the tool I am using, it sorts 5 values and then stops working - and is not working using negative values etc. Spoken to their development team and been told to upgrade - not an option.
Tl:dr - I need to sort with out using the inbuilt sort function.
I have two arrays
values = [1000, 2356, 3456778, 7645748, -2324, 348845.45, -2345666]
labels = ["ValA", "ValB", "ValC", "ValD", "ValE", "ValF", "ValG", "ValH"]
These need to be bined to show [1000,ValA, etc] with the largest value appearing first.
Without the inbuilt sort functionality, I am at a loss on how to proceed. I tried using the Math.Max to cycle through the array, but when I'd bined these two arrays the function stopped working.
I am not sure whether to bine these two individual arrays, or to keep them separate so that the sorting is simpler, remembering the original index and links to the label.
Any thoughts or questions wele, I am truly stumped and not a developer by trade so sure there is some kind soul out there who maybe able to support me.
Thanks
Expected output
[7645748, ValD]
[3456778, ValC]
[348845.45, ValF]
[2356, ValB]
[2324, ValE]
[1000, ValA]
[-2345666, ValG]
Problem in Javascript
I previously asked this question Javascript 2D array sorting - by numerical value with a solution provided using the sort function, which I have tried to use without sucsess. Turns out the sort function isn't correctly within the tool I am using, it sorts 5 values and then stops working - and is not working using negative values etc. Spoken to their development team and been told to upgrade - not an option.
Tl:dr - I need to sort with out using the inbuilt sort function.
I have two arrays
values = [1000, 2356, 3456778, 7645748, -2324, 348845.45, -2345666]
labels = ["ValA", "ValB", "ValC", "ValD", "ValE", "ValF", "ValG", "ValH"]
These need to be bined to show [1000,ValA, etc] with the largest value appearing first.
Without the inbuilt sort functionality, I am at a loss on how to proceed. I tried using the Math.Max to cycle through the array, but when I'd bined these two arrays the function stopped working.
I am not sure whether to bine these two individual arrays, or to keep them separate so that the sorting is simpler, remembering the original index and links to the label.
Any thoughts or questions wele, I am truly stumped and not a developer by trade so sure there is some kind soul out there who maybe able to support me.
Thanks
Expected output
[7645748, ValD]
[3456778, ValC]
[348845.45, ValF]
[2356, ValB]
[2324, ValE]
[1000, ValA]
[-2345666, ValG]
Share
edited May 23, 2017 at 12:13
CommunityBot
11 silver badge
asked Apr 4, 2013 at 10:34
2eus2eus
691 gold badge2 silver badges8 bronze badges
3
- Can you post the full output you're expecting? Saying "[1000,ValA, etc] with the largest value appearing first" really doesn't tell me at all what you're trying to do. – Anthony Grist Commented Apr 4, 2013 at 10:36
- Anthony Grist - Edited – 2eus Commented Apr 4, 2013 at 10:46
- en.literateprograms/Quicksort_%28JavaScript%29 or en.literateprograms/Merge_sort_%28JavaScript%29 – NimChimpsky Commented Apr 4, 2013 at 10:56
5 Answers
Reset to default 3Create an array of customr objects and pupoluate with the data
for (var i = 0; i < values.length; i++) {
myArray[i].value = values[i];
myArray[i].label = labels[i];
}
Then implement your own bubblesort algorithm :
for (var j = 0; j < myArray.length - 1; j++) {
for (var i = 0, swapping; i < myArray.length - 1; i++) {
if (myArray[i].value > myArray[i + 1].value) {
swapping = myArray[i + 1];
myArray[i + 1] = myArray[i];
myArray[i] = swapping;
};
};
};
var list = ['z','b', 'a', 'y'];
for (var j = 0; j < list.length - 1; j++) {
for (var i = 0, swapping; i < list.length - 1; i++) {
if (list[i]> list[i + 1]) {
swapping = list[i + 1];
list[i + 1] = list[i];
list[i] = swapping;
};
};
};
output will be = ["a", "b", "y", "z"]
This method is a very simple and easy way to solve a sort problem in an array without a sort() method.
function sortArray(arr) {
var temp = [];
if (Array.isArray(arr)) {
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length; j++) {
if (arr[i] < arr[j]) {
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
return arr;
}
}
console.log(sortArray([2, 6, 0, 4, 3, 4, 3, 5, 9, 6, 12, 43, 6]));
// It will work for both Python or JS code
let b = ["ValA", "ValB", "ValC", "ValD", "ValE", "ValF", "ValG", "ValH"]
for(let i=0;i<b.length-1;i++){
for(let j=i+1;j<b.length;j++)
{
if(b[j]<b[i])
{
temp = b[j]
b[j]=b[i]
b[i] = temp
}
}
}
console.log(b)
function sort(arr) {
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
var temp = arr[j]
arr[j] = arr[i]
arr[i] = temp
}
}
}
return arr;
}
本文标签: sortingJavascriptsort without sortStack Overflow
版权声明:本文标题:sorting - Javascript - sort without sort - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742220941a2435437.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论