2

how can i find the highest number in the array and sort the array so it has the highest numbers first?

I am using jQuery also, if that should make it easier. I cannot figure out how to sort it, and then outputs the sorted arrays with html into usersInRoom div.

Hope someone can help me though this!

I have readed a little bit about the "max" in javascript, but it was complicate, and didn't match my code.

I will sort the mydivs[.......]['number'] and not mydivs, because it contains an array in an array.

<div id="usersInRoom">
    <div class="entry in_room" id="userInRoom-2" style="background-color: rgb(255, 255, 255);">
        <table style="width:100%">
            <tbody>
            <tr>
                <td style="width:32px">
                    <img
                        src="https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn1/c33.33.414.414/s200x200/996882_221295918024677_1913237122_n.jpg"
                        style="height:32px;width:32px">
                </td>
                <td style="vertical-align:middle"><strong>Stefanie Pedersen</strong>
                </td>
                <td align="right" style="vertical-align:middle;color:#999"><span id="exp-2" class="exp">6</span> exp.
                </td>
            </tr>
            </tbody>
        </table>
    </div>
    <div class="entry in_room" id="userInRoom-1" style="background-color: rgb(155, 229, 162);">
        <table style="width:100%">
            <tbody>
            <tr>
                <td style="width:32px">
                    <img
                        src="https://fbcdn-profile-a.akamaihd.net/hprofile-ak-frc1/c176.49.608.608/s200x200/429356_10151257167066983_1091687280_n.jpg"
                        style="height:32px;width:32px">
                </td>
                <td style="vertical-align:middle"><strong>Bare Kald Mig Jesper</strong>
                </td>
                <td align="right" style="vertical-align:middle;color:#999"><span id="exp-1" class="exp">14</span> exp.
                </td>
            </tr>
            </tbody>
        </table>
    </div>
</div>

And here is my jQuery code to insert the HTML and the numbers into array, after this it should "just" sort it and outputs it again.

<script>
var numbers = [];
var mydivs = [];
var arr = $("#usersInRoom").find(".exp");
$.each(arr, function (e) {
    var eq = arr[e];
    console.log(eq);
    mydivs.push({
        "html": $(eq).parents(".entry").parents("div").html(),
        "number": parseInt($(eq).text())
    });
});

</script>

I WILL SORT THE mydivs[.......]['number'] <----

jesper
  • 15
  • 6
  • What does your array look like? You can use a method called `sort`. – putvande Aug 24 '13 at 10:03
  • possible duplicate of [How might I find the largest number contained in a JavaScript array?](http://stackoverflow.com/questions/1379553/how-might-i-find-the-largest-number-contained-in-a-javascript-array) -- [this answer](http://stackoverflow.com/a/1379568/218196) has exactly what you are asking for. – Felix Kling Aug 24 '13 at 10:19
  • My array? You can see the post and jsfiddle. I know exactly how to sort etc., but my array has an array in the array, and i want to sort it by the array-inarray "number" – jesper Aug 24 '13 at 10:21

5 Answers5

2

This one should work with more than 2 elements:

var aRows = jQuery.makeArray($('#usersInRoom').children());
aRows.sort(function(a,b) {
    return parseInt($(b).find('.exp').text()) -
           parseInt($(a).find('.exp').text());
});
$('#usersInRoom').empty().append(aRows);
asontu
  • 4,548
  • 1
  • 21
  • 29
1

I don't know what your array looks like but I think that you can use the JavaScript sort method.

var NumbersArray = [400,200,600,300,1000];
var SortedArray = NumbersArray.sort(function(a,b){return b-a});
alert(SortedArray);

http://www.w3schools.com/jsref/jsref_sort.asp

If you want to find the highest number before sorting you can try something like this:

var NumbersArray = [400,200,600,300,1000];
var MaxPreviousNumber = 0;
for (var i = 0; i <= NumbersArray.length; i++) {
    var MaxPreviousNumber = Math.max(MaxPreviousNumber, NumbersArray[i]);
}
qwertyuiop
  • 159
  • 1
  • 1
  • 7
  • Yes, i know that, but my array contains not only a number, but a html and a number, look at the post again and jsfiddle to see. – jesper Aug 24 '13 at 10:19
  • You can see this, wont work. var SortedArray = mydivs['number'].sort(function(a,b){return b-a}); – jesper Aug 24 '13 at 10:23
1

I think this is doing what you want:

http://jsfiddle.net/j8bLc/2/

var arr = $("#usersInRoom").find(".exp");
$.each(arr, function (key, el) {
    var curr = parseInt($(el).text());
    var top = parseInt($("#usersInRoom .exp").eq(0).text());
    if(curr > top) {
        $(el).closest(".entry").prependTo('#usersInRoom');
    }
});

But there might be a better solution with less DOM manipulations.

Alexander Scholz
  • 2,100
  • 1
  • 20
  • 35
0

To print your array data in reverse order you can find array length like,

for(var i=mydivs.length-1;i>=0;i++)
{
    console.log(mydivs[i]['html']);
}

Fiddle

Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
  • This is not the question, i know how to do that. The question is how to sort the array mydivs[i]['number'] in the highest order – jesper Aug 24 '13 at 10:20
0

If you will look closer here: http://www.w3schools.com/jsref/jsref_sort.asp, you can see that sort has a parameter - your own sorting function which you can use.

So, you can sort the array using you own comparing function

function compare(a,b) {
  if (a.number < b.number)
     return -1;
  if (a.number > b.number)
    return 1;
  return 0;
}

objs.sort(mydivs);

this will sort your array

Salvador Dali
  • 214,103
  • 147
  • 703
  • 753