Home > Net >  How to sort an array by if it contains a certain character in it in JavaScript? [JS]
How to sort an array by if it contains a certain character in it in JavaScript? [JS]

Time:10-07

I have an array called files:

['Cpp-New.html', 'dirname', 'dirname.html', 'dirname.py', 'HarryPotter', 'Java-New.html', 'poop.css', 'test01.html', 'Web-New.html']

which gets listed in html. (List)

but I would like to order/sort it so everything without a ."file extentsion" gets listed at the top. Currently the browser just receives the array in alphabetical order.

What is the best way to approach this? I tried doing some research but was unable to come up with any solutions.

Thank you.

CodePudding user response:

const files = ['Cpp-New.html', 'dirname', 'dirname.html', 'dirname.py', 'HarryPotter', 'Java-New.html', 'poop.css', 'test01.html', 'Web-New.html'];

const hasExtRegex = /\.[a-z0-9]{1,4}$/;
const sortedFiles = files.sort((a, b) => {
  const aHasExt = hasExtRegex.test(a);
  const bHasExt = hasExtRegex.test(b);
  return aHasExt - bHasExt;
});

console.log(sortedFiles);

CodePudding user response:

The way I do this is to create a score system, So for example in your case if we have a period I would prefix with a 1, if we don't I prefix with a 0, this will make the sort place the 0 prefix score's at the top.

eg..

const arr = ['Cpp-New.html', 'dirname', 'dirname.html', 'dirname.py', 'HarryPotter', 'Java-New.html', 'poop.css', 'test01.html', 'Web-New.html'];

function score(a) {
  if (a.includes('.')) return '1'   a;
  return '0'   a; //want these at top
}

arr.sort((a,b) => {
  return score(a).localeCompare(
    score(b), undefined, {sensitivity: 'base'});
});


console.log(arr);

CodePudding user response:

// a longer answer but this is without using sort of javascript

const values = ['Cpp-New.html', 'dirname', 'dirname.html', 'dirname.py', 'HarryPotter', 'Java-New.html', 'poop.css', 'test01.html', 'Web-New.html'];
let j = 1;
let i = 0;
while(i < values.length) {
    
    if(j >= values.length) break;
    if(values[i].includes(".")) {
        i  ;
        j  ;
        continue;
    }
    
    if(!values[i].includes(".") && values[j].includes(".")) {
        // swap
        let temp = values[i];
        values[i] = values[j];
        values[j] = temp;
        continue;
    }
    
    if(!values[i].includes(".") && !values[j].includes(".")) {
        j  ;
        continue;
    }
};

console.log(values);

CodePudding user response:

this is short, although this loses the alphabetical sorting

const arr = ['Cpp-New.html', 'dirname', 'dirname.html', 'dirname.py', 'HarryPotter', 'Java-New.html', 'poop.css', 'test01.html', 'Web-New.html'];

arr.sort((a) => a.includes(".") ? 1 : -1 );

console.log(arr);

Note that sort mutates the array, a solution without sort would be:

const newArr = arr.filter(a => !a.includes(".")).concat(arr.filter(a => a.includes(".")))

CodePudding user response:

This works even if your list is not originally sorted, and it only sorts once:

x = ['Cpp-New.html', 'dirname', 'dirname.html', 'dirname.py', 'HarryPotter', 'Java-New.html', 'poop.css', 'test01.html', 'Web-New.html']
re = /.*\.. $/ 
x.sort((a,b)=>(re.test(a)-re.test(b))*2   a.localeCompare(b))

Explaining:

  • (re.test(a)-re.test(b))*2 will give -2 (if a has an extension but b doesn't), 0 (if both either have or don't have extension) or 2 (if b has an extension but a doesn't). So, filenames with extensions will sort last.
  • a.localeCompare(b) will give either -1, 0 or 1, depending if a sorts alphabetically before, same, or after b (ignore case and accents).

Hence, even if a > b alphabetically, the regex test has a bigger weight in the score.

CodePudding user response:

Try this:

const arr = ['tshirt', 'polo', 'angular', 'react', 'HarryPotter', 'Java', 'Web'];

const score = (a) => {
  if (a.includes('.')) return '1'   a;
  return '0'   a;
}

arr.sort((a,b) => {
  return score(a).localeCompare(
    score(b));
});


console.log(arr);

  • Related