Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

soriting an array

dvass0107
Tera Contributor

Hi,

 

I am trying the below code in Background scripts, but unfinished, can somebody how to execute successfully?

this is sorting array elements: ( I want to achive this  without using  array methods)

var ar = [5, 3, 7, 1, 10];
for (i = 0; i <= ar.length; i++) {
    for (j = 0; j <= ar.length; j++) {
        if (ar[i] > j[j + 1]) {
        }
    }
}
var ar1 =[];
ar1.push(i);
gs.print("sort order" + ar);
--------------------------------------------------
Also please explaing to how put them in reverse order.
1 ACCEPTED SOLUTION

Deepak Shaerma
Mega Sage

Hi @dvass0107 

Please use basic bubble sort method,

 

Ascending Order

 

var ar = [5, 3, 7, 1, 10];

for (var i = 0; i < ar.length; i++) {

    for (var j = 0; j < ar.length - 1; j++) {

        if (ar[j] > ar[j + 1]) {

            // Swap elements

            var temp = ar[j];

            ar[j] = ar[j + 1];

            ar[j + 1] = temp;

        }

    }

}

 

gs.print("Sorted array in ascending order: " + ar);

 

 

 

var ar = [5, 3, 7, 1, 10];

for (var i = 0; i < ar.length; i++) {

    for (var j = 0; j < ar.length - 1; j++) {

        if (ar[j] > ar[j + 1]) {

            // Swap elements

            var temp = ar[j];

            ar[j] = ar[j + 1];

            ar[j + 1] = temp;

        }

    }

}

 

gs.print("Sorted array in ascending order: " + ar);

 

Reverse Order

To reverse the sorted array, we can simply swap the elements starting from each end of the array.

 

// Reverse the array

for (var i = 0; i < Math.floor(ar.length / 2); i++) {

    var temp = ar[i];

    ar[i] = ar[ar.length - 1 - i];

    ar[ar.length - 1 - i] = temp;

}

 

gs.print("Sorted array in descending order: " + ar);

 

 

please mark this Helpful and Accepted Solution if this helps you. Your action will help me and the community in understanding same requirements.

Thanks & Regards

Deepak Sharma

View solution in original post

2 REPLIES 2

Deepak Shaerma
Mega Sage

Hi @dvass0107 

Please use basic bubble sort method,

 

Ascending Order

 

var ar = [5, 3, 7, 1, 10];

for (var i = 0; i < ar.length; i++) {

    for (var j = 0; j < ar.length - 1; j++) {

        if (ar[j] > ar[j + 1]) {

            // Swap elements

            var temp = ar[j];

            ar[j] = ar[j + 1];

            ar[j + 1] = temp;

        }

    }

}

 

gs.print("Sorted array in ascending order: " + ar);

 

 

 

var ar = [5, 3, 7, 1, 10];

for (var i = 0; i < ar.length; i++) {

    for (var j = 0; j < ar.length - 1; j++) {

        if (ar[j] > ar[j + 1]) {

            // Swap elements

            var temp = ar[j];

            ar[j] = ar[j + 1];

            ar[j + 1] = temp;

        }

    }

}

 

gs.print("Sorted array in ascending order: " + ar);

 

Reverse Order

To reverse the sorted array, we can simply swap the elements starting from each end of the array.

 

// Reverse the array

for (var i = 0; i < Math.floor(ar.length / 2); i++) {

    var temp = ar[i];

    ar[i] = ar[ar.length - 1 - i];

    ar[ar.length - 1 - i] = temp;

}

 

gs.print("Sorted array in descending order: " + ar);

 

 

please mark this Helpful and Accepted Solution if this helps you. Your action will help me and the community in understanding same requirements.

Thanks & Regards

Deepak Sharma

Nisha15
Mega Sage

Can you try this?

var ar = [5, 3, 7, 1, 10];

// Bubble Sort implementation
for (var i = 0; i < ar.length; i++) {
    for (var j = 0; j < ar.length - 1; j++) {
        if (ar[j] > ar[j + 1]) {
            var temp = ar[j];
            ar[j] = ar[j + 1];
            ar[j + 1] = temp;
        }
    }
}

gs.print("Sorted order: " + ar);