0

I have an array a=["Apple","Mango","apple","mango"] .If I use the a.sort() the result is ["Apple", "Mango", "apple", "mango"]

But What i desire is

Apple,apple,Mango,mango

Remember It is not case inSesitive search as whatever the order of given element in the array be ,the output should be as

Apple apple Mango mango Means the capital letter should precedes the smaller one

2
  • I think it has something to do with the case (apple vs Apple). Commented Aug 19, 2014 at 11:49
  • have you considered writing your own compareFn? Commented Aug 19, 2014 at 11:50

1 Answer 1

0

Use This

function alphabetical(a, b)
{
     var A = a.toLowerCase();
     var B = b.toLowerCase();
     if (A < B){
        return -1;
     }else if (A > B){
       return  1;
     }else{
       return 0;
     }
}

$(document).ready(function(){
var colors = new Array("Apple","Mango","apple","mango");

colors.sort(alphabetical);

var cj = colors.join(", ");
alert(cj);
})

DEMO

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

5 Comments

It is not case insensitive search, I updated my question...
my output is not same as you are desiring?
taking array as ("apple","Mango","Apple","mango") output (apple,Apple,Mango,mango) but it should be (Apple,apple,Mango,mango)
@Roshanjha check my updated demo
Sorry have asked this question again because this has been marked as duplicated.And your previous answer was not according to the requirement, Anyway thanks for your help

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.