简体   繁体   中英

Im trying to filter then sort an array of numbers and string but my code isn't working

I have an array of letters and numbers.

let sortLetters = [ 'R', '1', 'U', '1', 'N', '1', 'D', '1', 'M', '1', 'C', '1' ]

I want to sort the types alphabetically, then return the first letter I come across.

My expected output should be C .

Currently my code sorts all types correctly but if I try to filter the numbers out it doesn't return what I need. Any help is appreciated.

let sortLetters = [ 'R', '1', 'U', '1', 'N', '1', 'D', '1', 'M', '1', 'C', '1' ]

const sortBothType =(arr)=> {
  return [...arr].sort((a, b) => a.localeCompare(b));
}

const getFirstLetter = (arr) =>{
  let result = ''
  for(let x = 0; x <= arr.length;x++){
    if( parseInt(arr[x]) === 'string'){
      return arr[x]
    }
  }
}

let sortedType = sortBothType(sortLetters)
sortedType
getFirstLetter(sortedType)
let finalResult = getFirstLetter(sortedType)


You could take the first found NaN by using Array#find and isNaN .

 const sortBothType = ([...array]) => array.sort((a, b) => a.localeCompare(b)); getFirstLetter = array => array.find(isNaN); let sortLetters = [ 'R', '1', 'U', '1', 'N', '1', 'D', '1', 'M', '1', 'C', '1'], sortedType = sortBothType(sortLetters); console.log(getFirstLetter(sortedType)); console.log(sortedType); 

First filter away all digits, then sort, finally access the first array member. This will give you your C in the example:

sortLetters.filter(x=>/\D/.test(x)).sort()[0]

Since you only need the letter, you can use Array.reduce() to get the "smallest" letter. You can ignore numbers by using isNaN() , and taking the items that return true .

 const sortLetters = ['R', '1', 'U', '1', 'N', '1', 'D', '1', 'M', '1', 'C', '1' ] const result = sortLetters.reduce((r, char) => !isNaN(r) || (isNaN(char) && char < r) ? char : r ) console.log(result) 

You are comparing the result of parseInt() to 'string' , use isNaN() instead.

Also, if you filter out the numbers first, you won't have to copy the array and you can use sort() with no arguments. Then use shift() to get the first letter:

 const letters = [ 'R', '1', 'U', '1', 'N', '1', 'D', '1', 'M', '1', 'C', '1' ]; console.log(letters.filter(isNaN).sort().shift()); 

let sortLetters = ', '1', 'U', '1', 'N', '1', 'D', '1', 'M', '1', 'C', '1']

console.log(
    sortLetters.map(item => isNaN(parseInt(item)) ? item : null).sort()[0]
);

explanation

1 In map fn, we check for every item, if is number or not. ParseInt method is suitable, when you want check if string value have number inside or not. If not, returns isNaN - so is string.

2 Ternary operator ?: in map function help us to create new array with result of check, that we made in 1.

3 Map method returns array with only not number chars, so we can use sort method to sort alphabetically.

4 [0] - just return firstitem from sorted list of letters.

  • We can filter out the numbers by converting the alphabets in the array to NaN . Appending a arithmetic operator like + converts a non-number to NaN

  • Then filter them using Number.isNaN() and sort the filtered alphabet array in which the first element would be C .

 let sortLetters = [ 'R', '1', 'U', '1', 'N', '1', 'D', '1', 'M', '1', 'C', '1' ] const sortBothType =(arr)=> { return arr.filter(a => isNaN(+a)).sort((a,b) => a.localeCompare(b)); } let [firstLetter, ...rest] = sortBothType(sortLetters); console.log(firstLetter) 

You could use the OR operator in the compareFunction to group the sorted alphabets and numbers separately:

 let array = [ 'R', '1', 'U', '1', '2', 'N', '1', 'D', '1', 'M', '1', 'C', '1' ] let customSort = [...array].sort((a, b) => isNaN(b) - isNaN(a) || a.localeCompare(b)) console.log(customSort) console.log(customSort[0]) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM