Answers for "js reverse sort digits"

0

js sort numbers descending order

// Sort Numbers in Descending Order
function sortDescending(num) {
	return Number(num.toString().split('').sort((a, b) => b - a).join(''));
//or: return parseInt(num.toString().split('').sort().reverse().join(''));
}

console.log(sortDescending(123)); // 321
console.log(sortDescending(1254859723)); // 9875543221
Posted by: Guest on March-12-2021
0

sort string mixed with numbers javascript

var reA = /[^a-zA-Z]/g;
var reN = /[^0-9]/g;

function sortAlphaNum(a, b) {
  var aA = a.replace(reA, "");
  var bA = b.replace(reA, "");
  if (aA === bA) {
    var aN = parseInt(a.replace(reN, ""), 10);
    var bN = parseInt(b.replace(reN, ""), 10);
    return aN === bN ? 0 : aN > bN ? 1 : -1;
  } else {
    return aA > bA ? 1 : -1;
  }
}
console.log(
["A1", "A10", "A11", "A12", "A2", "A3", "A4", "B10", "B2", "F1", "F12", "F3"].sort(sortAlphaNum)
)
Posted by: Guest on August-16-2021
0

js sort numbers descending order

// Sort Numbers in Descending Order
function sortDescending(num) {
	return Number(num.toString().split('').sort((a, b) => b - a).join(''));
//or: return parseInt(num.toString().split('').sort().reverse().join(''));
}

console.log(sortDescending(123)); // 321
console.log(sortDescending(1254859723)); // 9875543221
Posted by: Guest on March-12-2021
0

sort string mixed with numbers javascript

var reA = /[^a-zA-Z]/g;
var reN = /[^0-9]/g;

function sortAlphaNum(a, b) {
  var aA = a.replace(reA, "");
  var bA = b.replace(reA, "");
  if (aA === bA) {
    var aN = parseInt(a.replace(reN, ""), 10);
    var bN = parseInt(b.replace(reN, ""), 10);
    return aN === bN ? 0 : aN > bN ? 1 : -1;
  } else {
    return aA > bA ? 1 : -1;
  }
}
console.log(
["A1", "A10", "A11", "A12", "A2", "A3", "A4", "B10", "B2", "F1", "F12", "F3"].sort(sortAlphaNum)
)
Posted by: Guest on August-16-2021
0

js sort numbers descending order

// Sort Numbers in Descending Order
function sortDescending(num) {
	return Number(num.toString().split('').sort((a, b) => b - a).join(''));
//or: return parseInt(num.toString().split('').sort().reverse().join(''));
}

console.log(sortDescending(123)); // 321
console.log(sortDescending(1254859723)); // 9875543221
Posted by: Guest on March-12-2021
0

sort string mixed with numbers javascript

var reA = /[^a-zA-Z]/g;
var reN = /[^0-9]/g;

function sortAlphaNum(a, b) {
  var aA = a.replace(reA, "");
  var bA = b.replace(reA, "");
  if (aA === bA) {
    var aN = parseInt(a.replace(reN, ""), 10);
    var bN = parseInt(b.replace(reN, ""), 10);
    return aN === bN ? 0 : aN > bN ? 1 : -1;
  } else {
    return aA > bA ? 1 : -1;
  }
}
console.log(
["A1", "A10", "A11", "A12", "A2", "A3", "A4", "B10", "B2", "F1", "F12", "F3"].sort(sortAlphaNum)
)
Posted by: Guest on August-16-2021

Browse Popular Code Answers by Language