locked
Array sorting in Jaquery/Javascript RRS feed

  • Question

  • User2038048451 posted

    I have array as shown in the below code with data,

    var data = [];
    data.push('testing1-orange-0');
    data.push('testing2-apple-1');
    data.push('target-orange-2');
    data.push('solid-orange-3');
    data.push('lenova-orange-4');
    data.push('dell-apple-9');
    data.push('samsung-orange-8');
    data.push('testing-orange-11');
    data.push('testing-orange-10');
    data.sort();

    when i do sorting it is giving below output

    'testing1-orange-0'
    'testing2-apple-1'
    'target-orange-2'
    'solid-orange-3'
    'lenova-orange-4'
    'dell-apple-9'
    'samsung-orange-8'
    'testing-orange-11'
    'testing-orange-10'

    where as i want output like below

    'testing1-orange-0'
    'testing2-apple-1'
    'target-orange-2'
    'solid-orange-3'
    'lenova-orange-4'
    'samsung-orange-8'
    'dell-apple-9'
    'testing-orange-10'
    'testing-orange-11'

    Tuesday, April 24, 2018 6:34 AM

All replies

  • User-369506445 posted

    hi

    please try below code :

    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" language="javascript">
    
    
        $(document).ready(function () {
            var data = [];
            data.push('testing1-orange-0');
            data.push('testing2-apple-1');
            data.push('target-orange-2');
            data.push('solid-orange-3');
            data.push('lenova-orange-4');
            data.push('dell-apple-9');
            data.push('samsung-orange-8');
            data.push('testing-orange-11');
            data.push('testing-orange-10');
            data.sort();
    
    
            debugger;
            data=data.sort(function (a, b) {
                var textA = a.toUpperCase();
                var textB = b.toUpperCase();
                return (textA > textB) ? -1 : (textA < textB) ? 1 : 0;
            });
    
            for (var i = 0; i < data.length; ++i) {
                alert(data[i]);
            }
    
        });
        
    </script>

    Tuesday, April 24, 2018 6:57 AM
  • User2038048451 posted

    Hi,

    it is giving below out put

    testing2-apple-1
    testing1-orange-0
    testing-orange-11
    testing-orange-10
    target-orange-2
    solid-orange-3
    samsung-orange-8
    lenova-orange-4
    dell-apple-9

    but i required data as below

    testing1-orange-0
    testing2-apple-1
    target-orange-2
    solid-orange-3
    lenova-orange-4
    samsung-orange-8
    dell-apple-9
    testing-orange-10
    testing-orange-11

    Tuesday, April 24, 2018 8:55 AM
  • User36583972 posted

    Hi nrk_hi,

    but i required data as below

    testing1-orange-0
    testing2-apple-1
    target-orange-2
    solid-orange-3
    lenova-orange-4
    samsung-orange-8
    dell-apple-9
    testing-orange-10
    testing-orange-11

    From your description, you want sort your data according to the last digit of the string.

    You can refer the following code.

          <script type="text/javascript">  
             $(document).ready(function () {  
                   var data = [];
            data.push('testing1-orange-0');
            data.push('testing2-apple-1');
            data.push('target-orange-2');
            data.push('solid-orange-3');
            data.push('lenova-orange-4');
            data.push('dell-apple-9');
            data.push('samsung-orange-8');
            data.push('testing-orange-11');
                 data.push('testing-orange-10');
    
                 data = data.sort(function (a, b) {  
                     a = a.substr((a.lastIndexOf("-") + 1));
                     b = b.substr((b.lastIndexOf("-") + 1));
                     return a - b;
    
                 });  
      
                 $.each(data, function (index, value) {  
                     $('p.All').append("<tr><td>" + value + "</td></tr>");  
      
                 });  
             });  
         </script>  

    Best Regards,

    Yong Lu

    Wednesday, April 25, 2018 2:51 AM
  • User-369506445 posted

    hi

    pleases try below code :

    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            var data = [];
            data.push('testing1-orange-0');
            data.push('testing2-apple-1');
            data.push('target-orange-2');
            data.push('solid-orange-3');
            data.push('lenova-orange-4');
            data.push('dell-apple-9');
            data.push('samsung-orange-8');
            data.push('testing-orange-11');
            data.push('testing-orange-10');
    
            data = data.sort(function (a, b) {
                a = a.split('-')[2];
                b = b.split('-')[2];
                return a - b;
    
            });
    
            for (var i = 0; i < data.length; ++i) {
                alert(data[i]);
            }
        });
    </script>

    Wednesday, April 25, 2018 4:52 AM