0

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 combined 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 combined these two arrays the function stopped working.

I am not sure whether to combine 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 welcome, 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]
3

5 Answers 5

3

Create 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;
        };
    };
};
Sign up to request clarification or add additional context in comments.

6 Comments

I took "I need to sort with out using the inbuilt sort function." to mean that Array.sort() isn't an option.
I took it to mean the default functionality (of converting to strings and comparinging lexigraphickally) is not applicable.
That doesn't really make sense when you take into account the answer to the other question he asked (linked in this one), which provides two suggestions, one of which uses Array.sort() on an array of objects with a custom comparison function.
@AnthonyGrist thats probably why this answer hasn't got any vote ups then
Yes the .sort function is unavailable to me, hence the reason I can not use the solution provided previously.
|
0

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"]

Comments

0

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]));

Comments

0
   // 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)

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0
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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.