Home > Software design >  How to Sort Alphabets on right-side ascending and number on left side descending (string from user i
How to Sort Alphabets on right-side ascending and number on left side descending (string from user i

Time:03-09

I'm working on a function to accept input from a user. When user submit, it will sort ascending for alphabet and number on the right descending. For example if user input is :

a98 @#$64b,ce>75d l3kj gh and the output should be : abcdeghjkl9876543

So here is what I've done so far :

    let sortNumButton = document.getElementById('sortNumButton');
let sortOutputContainer = document.getElementById('sortOutputContainer');
let inputField = document.getElementById('inputField');

sortNumButton.addEventListener('click', function(){
    let string = document.createElement('p')
    string.innerText = inputField.value
    .split('')
    .sort((a,b) => isFinite(a) - isFinite(b) || a > b || -(a > b))
    .join('')
    .replace(/[^a-zA-Z-0-9]/g, "")
    sortOutputContainer.appendChild(string)
    inputField.value = ""
})

CodePudding user response:

One way of doing this is just to create a score system for the comparison.

For example, if we are a character, we can just use the charCodeAt to get it's charcode, just return this as positive value.

If we are then a number, we could just return it as a number say 1000 then subtract it's value.. ps just doing 1000-somechar, will also convert to number so no parseInt etc needed.

eg..

const txt = 'a98 @#$64b,ce>75d l3kj gh';

const isNumeric = /[0-9]/;
const isAlpha = /[a-z]/;
const isAlphaNumeric = /[a-z0-9]/;

function score(v) {
  if (isAlpha.test(v)) return v.charCodeAt(0);
  else return 1000-v;
}

console.log(
  txt.split('')
    .filter(t => isAlphaNumeric.test(t))
    .sort((a,b) => score(a) - score(b))
    .join('')
)

Another option, if we have limited chars we could just create a simple lookup table and then sort on this..

eg..

const txt = 'a98 @#$64b,ce>75d l3kj gh';

const isAlphaNumeric = /[a-z0-9]/;

const lookup = 'abcdefghijklmnopqrstuvwxyz9876543210';

function score(v) {
  return lookup.indexOf(v);
}

console.log(
  txt.split('')
    .filter(t => isAlphaNumeric.test(t))
    .sort((a,b) => score(a) - score(b))
    .join('')
)

CodePudding user response:

You can use regex to pull out the numbers and letters separately and then you can perform the sorting.

Working Demo :

// Input string
const str = 'a98 @#$64b,ce>75d l3kj gh';

// letters string with sorting in ascending order.
const lettersString = str.match(/[a-z]/g).sort().join('');

// Numbers string with sorting in descending order.
const numbersString = str.match(/[0-9]/g).sort().reverse().join('');

// Result
console.log(`${lettersString}${numbersString}`);

CodePudding user response:

You can use a regular expression to pull out the numbers and letters with match. This returns an array that you can then join into a string for output.

This example is a little more functional that is probably needed but it shows a different approach.

const str = 'a98 @#$64b,ce>75d l3kj gh';

// `comparator` gets called with a `type`
// argument and returns a function that gets used
// as the actual sort comparator.
// We use `localCompare` to return a sorted array
// based on the type.
function comparator(type) {
  return function (a, b) {
    return (
      type === 'str'
      ? a.localeCompare(b) > b.localeCompare(a)
      : a.localeCompare(b) < b.localeCompare(a)
    );
  }
}

// `sort` accepts a type argument and
// returns a string based on calling
// the sort method on the array which calls the
// comparator function with the type, and once that
// process is complete returns a string
function sort(data, type) {
  return data.sort(comparator(type)).join('');
}

// `match` returns an array, so we pass that to
// the sort function with a type parameter
const letters = sort(str.match(/[a-z]/g), 'str');
const numbers = sort(str.match(/[0-9]/g), 'num');

console.log(`${letters}${numbers}`);

Additional information

  • Related