1

i am this array:

var notifiche_eq= ["10/avdvidv","15/jcbscisb","7/ciudsu"];

and i would this output:

["15/jcbscisb","10/avdvidv","7/ciudsu"] 

so based on numeric characters of each element.

I tried this code but doesn't work:

var notifiche_eq= ["10/avdvidv","15/jcbscisb","7/ciudsu"];

notifiche_eq.sort(function(a, b) {
    var milliA= notifiche_eq[a].split('/');
    var milliB= notifiche_eq[b].split('/');

    milliA= (milliA[0])+0;
    milliB= (milliB[0])+0;

    if(milliB- milliA){
     return b-a;
    }

}); 

alert(notifiche_eq);

here there's jsfiddle: https://jsfiddle.net/13tLjqc0/8/

I hope you can help me and sorry for my english :/

6 Answers 6

1

You don't need to use notifiche_eq[a]. a is already your item.

I optimized your code, so that it handles >, < and ==

var notifiche_eq= ["10/avdvidv","15/jcbscisb","7/ciudsu"];

notifiche_eq.sort(function(a, b) {
  return (+a.split('/')[0] > +b.split('/')[0] ? -1 : (+a.split('/')[0] < +b.split('/')[0] ? 1 : 0));
}); 

console.log(notifiche_eq);

Version without any ternary operators like asked in the comments :

var notifiche_eq= ["10/avdvidv","15/jcbscisb","7/ciudsu"];

notifiche_eq.sort(function(a, b) {
  if(+a.split('/')[0] > +b.split('/')[0]) return -1;
  else if(+a.split('/')[0] < +b.split('/')[0]) return 1;
  else return 0;
}); 

console.log(notifiche_eq);

Sign up to request clarification or add additional context in comments.

1 Comment

please can you show me code not using ternary operator ? Thanks a lot :)
1

a is already the item of the array, instead of its index so this line is wrong

var milliA= notifiche_eq[a].split('/');

Instead just simply

  • split the item of the array
  • get first index value
  • convert that to number using unary +

i.e.

notifiche_eq.sort(function(a, b) {
  return +b.split('/')[0] - +a.split('/')[0];
});

Comments

1

Issues you had...

  • notifiche_eq[a] and notifiche_eq[b] should be just a and b.
  • Just return b-a.

var notifiche_eq= ["10/avdvidv","15/jcbscisb","7/ciudsu"];

notifiche_eq = notifiche_eq.sort(function(a, b) {
    var milliA= a.split('/');
    var milliB= b.split('/');

    milliA= +milliA[0];
    milliB= +milliB[0];

    return milliB - milliA;

}); 

console.log(notifiche_eq);

You can do some fancy things on your code to make it look good... like..

notifiche_eq.sort((a, b) => +b.split('/')[0] - +a.split('/')[0]);

Comments

1

var notifiche_eq = ["10/avdvidv", "15/jcbscisb", "7/ciudsu"];

// es5
notifiche_eq.sort(function (a, b) {
    return b.split('/').shift() - a.split('/').shift()
});
// es6
notifiche_eq.sort((a, b) => b.split('/').shift() - a.split('/').shift());

console.log(notifiche_eq)

Comments

1

Split by /, take the first element and convert it to number by using +

var notifiche_eq= ["10/avdvidv", "15/jcbscisb", "7/ciudsu"];
notifiche_eq.sort((a, b) => +b.split('/')[0] - +a.split('/')[0]);
console.log(notifiche_eq);

1 Comment

Good answer, you should perhaps add an explanation about why notifiche_eq[a] is incorrect
1

You could use a regular expression for getting the starting digits of the string.

var notifiche_eq = ["10/avdvidv","15/jcbscisb","7/ciudsu"];

notifiche_eq.sort(function (a, b) {
    function getValue(s) { return s.match(/^\d+/)[0]; }
    return getValue(b) - getValue(a);
});

console.log(notifiche_eq);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.