how to sort array least to greatest javascript stACK


function sortArray(array) {
var temp = 0;
for (var i = 0; i < array.length; i++) {
for (var j = i; j < array.length; j++) {
if (array[j] < array[i]) {
temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}
}
return array;
}

console.log(sortArray([3,1,2]));

www.codegrepper.com
www.stackoverflow.com

how to sort array without using sort method in javascript


function bubbleSort(array) {
var done = false;
while (!done) {
done = true;
for (var i = 1; i < array.length; i += 1) {
if (array[i - 1] > array[i]) {
done = false;
var tmp = array[i - 1];
array[i - 1] = array[i];
array[i] = tmp;
}
}
}

return array;
}

var numbers = [12, 10, 15, 11, 14, 13, 16];
bubbleSort(numbers);
console.log(numbers);

www.codegrepper.com
www.stackoverflow.com

how to sort by data in chart js


const arrayOfObj = chartLabels.map(function (d, i) {
return {
label: d,
data: chartValues[i] || 0,
}
})

const sortedArrayOfObj = arrayOfObj.sort(function (a, b) {
return b.data - a.data
})

let newArrayLabel = []
let newArrayData = []
sortedArrayOfObj.forEach(function (d) {
newArrayLabel.push(d.label)
newArrayData.push(d.data)
})

www.codegrepper.com
www.stackoverflow.com

how to sort string aray in ts


// TypeScript
const value = ['2020', '2017', '2019', '2018']
value.sort((a,b) => a < b ? -1 : a > b ? 1 : 0) // Ascending
value.sort((a,b) => a < b ? 1 : a > b ? -1 : 0) // descending

// javaScript
const value = ['2020', '2017', '2019', '2018']
Array.from(value).sort((a,b) => a-b) // Ascending
Array.from(value).sort((a,b) => b-a) // descending

www.codegrepper.com
www.stackoverflow.com