1

I am trying to sort my array, both ascending and descending. I cant seem to figure out how to do this correctly though. I tried with different methods, but none worked. This is supposed to happen when i click two button, one with the id "stigande"ascending and one with the id "fallande"descending. Any solution?

Javascript

$(document).ready(function(){

var $films = [];

// ändra bakgrundsfärg för textfield 
$('#name').keyup(function(){
            $('#name').css('background-color', 'white');
});

// ändra bakgrundsfärg för select lista
$('#options').change(function(){
            $('#options').css('background-color', 'white');
});

// kör funktionen när knappen med id button klickas
$("#button").click(function(){
    var $titelBetyg = [];

    var $titel = $('#name').val();
    var $betyg = $('#options').val();

    // kontrollera om textfield är tom
    if($titel == ""){
        $('#name').css('background-color', 'red');
        alert("Mata in titel.....");
    }

    // kontrollera om select lista inte är vald
    else if($betyg == "0"){
        $('#options').css('background-color', 'red');
        alert("Välj betyg....");
    }
    else{

        $titelBetyg.push($titel);
        $titelBetyg.push($betyg);
        $films.push($titelBetyg);

        var $ul = $('#rightbar ul').empty();
        if (!$ul.length){ // om ul inte finns
            $ul = $("<ul>").appendTo('#rightbar'); // skapa en ny
        }

        // loopa igenom arrayen och placera innehållet i ul och li
        for (var i=0; i<$films.length; i++){
            $ul.append("<li>" + $films[i][0] + " " + $films[i][1] + "</li>");
        }

        $('#form').get(0).reset();

    }
});

// kör funktionen när knappen med id stigande klickas
$('#stigande').click(function(a,b){
    $films.sort();
});

// kör funktionen när knappen med id fallande klickas
$('#fallande').click(function(){
    $films.reverse();   
});

});
DrWooolie
  • 7,637
  • 7
  • 20
  • 19
  • From the looks of your code, the $films array is an array of objects - am I right? If so then using `sort()` on its own won't work, but this might help: http://stackoverflow.com/questions/5503900/how-to-sort-an-array-of-objects-with-jquery-or-javascript – MassivePenguin Feb 27 '13 at 16:09
  • its a nestled array. ill look into it, thanks! – DrWooolie Feb 27 '13 at 16:10
  • or better yet, its a multidimensional array. – DrWooolie Feb 27 '13 at 16:16

1 Answers1

4

As you're sorting an array of arrays rather than sorting just a series of names, you'll need to do something slightly different for sorting.

Try using something such as this:

function arrSort(arr, subkey){
  //Default to 0 if no subkey is set
  subkey = (subkey===undefined?0:subkey);

  var a = arr.slice(0),
      b = [], x;

  //For each section in the array, create an array containing whatever we are trying to sort by and our unique ID
  for (x in a){
    b[x]=[a[x][subkey], x];
  }

  b=b.sort();

  //Wipe out all the data that's currently in arr!
  arr.splice(0, arr.length);

  for (x in b){
    arr.push(a[b[x][1]]);
  }
  return arr;
}

I believe you'd be wanting to sort by $titel so you'd want to implement this something like this:

// kör funktionen när knappen med id stigande klickas
$('#stigande').click(function(a,b){
  arrSort($films, 0);
});

// kör funktionen när knappen med id fallande klickas
$('#fallande').click(function(){
  arrSort($films, 0);
  $films.reverse();   
});
Doug
  • 3,312
  • 1
  • 24
  • 31
  • That shouldn't be relevant , as long as you sort it before you append it. Here's an example of it's use: http://jsfiddle.net/jgJmk/1/ – Doug Feb 28 '13 at 17:22