0
<div id='panelb'>
  <div class='item'>Ćao</div>
  <div class='item'>Bella</div>
  <div class='item'>Mare</div>
</div>

I sort the above divs using following code:

js

var items = $('.item').get();
items.sort(function(a, b) {
    return $(a).text().localeCompare($(b).text());
});
$('#panelb').append(items);

It works, but what I need is to check if items are sorted or not, before the above procedure.

Something like:

if ('.item').are(:sorted) {alert ('sorted');}
else {alert ('not sorted');}

Any idea?

qadenza
  • 9,025
  • 18
  • 73
  • 126
  • 1
    I would imagine that any check to see if they're sorted would take as much time as it would to sort them in the first place. – j08691 Jan 27 '17 at 21:48
  • @j08691, doesn't matter, I need to place this info to the user – qadenza Jan 27 '17 at 21:49
  • 1
    Well then, get the items, make a copy, sort the copy, compare the two. – Heretic Monkey Jan 27 '17 at 21:50
  • @j08691 No it wouldn't. Checking if the elements are ordered is O(n) (lineal time). The best sorting algorithms are O(n log n) on average. Although in doesn't matter much for so few elements. – abl Jan 27 '17 at 21:59

2 Answers2

1
var testOutOfOrder = function(item, idx) {
  if (idx === 0) return false;
  return ($(item).text().localeCompare($(items[idx - 1]).text()) < 0);
};
if (items.some(testOutOfOrder)) {
  ... do sort here
}

This will test if any are out of order. You could do a standard for loop and break as well if your environment doesn't support the 'some()' method.

var testOutOfOrder = function(items) {
  var isOutOfOrder = false;
  for (var i = 0; i < items.length; i++) {
    if ($(item).text().localeCompare($(items[idx - 1]).text()) < 0) {
      isOutOfOrder = true;
      break;
    }
  }
  return isOutOfOrder;
};

if (testOutOfOrder(items)) {
  ... sort logic here
}
rasmeister
  • 1,986
  • 1
  • 13
  • 19
1

Well, based on the comment from Mike McCaughan, I just wanted to give it a try:

$(document).ready(function() {

  var items = $('.item'), // get the items
      clonedItems = items.clone(), // copy the items
      isSorted = false;

  // sort the copy
  clonedItems.sort(function(a, b) {
    return $(a).text().localeCompare($(b).text());
  });

  // compare items
  isSorted = $.arrayCompare(items, clonedItems);

  // if not sorted from the first time, show them sorted
  if (!isSorted) {
    $('#panelb').html(clonedItems);
  }
  
});

// Obtained from: http://stackoverflow.com/a/5186565/1178686
jQuery.extend({
  arrayCompare: function(arrayA, arrayB) {
    if (arrayA.length != arrayB.length) {
      return false;
    }
    // sort modifies original array
    // (which are passed by reference to our method!)
    // so clone the arrays before sorting
    var a = jQuery.extend(true, [], arrayA);
    var b = jQuery.extend(true, [], arrayB);
    a.sort();
    b.sort();
    for (var i = 0, l = a.length; i < l; i++) {
      if (a[i] !== b[i]) {
        return false;
      }
    }
    return true;
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="panelb">
  <div class="item">Ćao</div>
  <div class="item">Bella</div>
  <div class="item">Mare</div>
</div>
Oscar Jara
  • 14,129
  • 10
  • 62
  • 94