$(function() {

    var app_menu = $('#apps ul');
    app_menu.hide();
    if(!app_menu.length)
        return;

    // restore existing order
    $.get(OC.filePath('apporder','ajax','order.php'), { requesttoken: oc_requesttoken}, function(data) {
        var json = data.order;
        try {
            var order = JSON.parse( json ).reverse();
        } catch (e) {
            var order = []
        }
        if (order.length==0) {
            $('#apps ul').show();
            return;
        }
        available_apps = {};
        app_menu.find('li').each(function() {
            var id =  $(this).find('a').attr('href');
            available_apps[id] = $(this);
        });
        $.each(order,function(order,value) {
            app_menu.prepend(available_apps[value]);
        })
        $('#apps ul').show();
    });

    // make app menu sortable
    $( "#apps ul" ).sortable({
        handle: 'img',
        stop: function( event, ui ) {
            var items = [];
            $("#apps ul").children().each(function(i,el){
                var item = $(el).find('a').attr('href');
                items.push(item)
            })

            var json = JSON.stringify(items);
            localStorage.setItem("apporder-order", json);
            $.get(OC.filePath('apporder','ajax','personal.php'), { order: json, requesttoken: oc_requesttoken}, function(data) {
                $(event.srcElement).fadeTo('fast', 0.5).fadeTo('fast', 1.0);
            });
        }
    });

    // Sorting inside settings
    $( "#appsorter" ).sortable({
        stop: function( event, ui ) {
            var items = []
                $("#appsorter").children().each(function(i,el){
                    var item = $(el).find('a').attr('href');
                    items.push(item)
                })
            var json = JSON.stringify(items);
            $.get(OC.filePath('apporder','ajax','admin.php'), { order: json, requesttoken: oc_requesttoken}, function(data) {
                $(event.srcElement).effect("highlight", {}, 1000);
            });
        }
    });
});

@@ -272,16 +272,39 @@
 .order;%0A
+        var order = %5B%5D%0A
         
@@ -389,36 +389,32 @@
 ) %7B%0A            
-var 
 order = %5B%5D%0A     
@@ -399,32 +399,33 @@
       order = %5B%5D
+;
 %0A        %7D%0A     
@@ -447,10 +447,13 @@
 ngth
+ 
 ==
+= 
 0) %7B
@@ -800,32 +800,33 @@
 ue%5D);%0A        %7D)
+;
 %0A        $('#app
@@ -1138,32 +1138,33 @@
 items.push(item)
+;
 %0A            %7D)%0A
@@ -1162,16 +1162,17 @@
       %7D)
+;
 %0A%0A      
@@ -1819,16 +1819,17 @@
       %7D)
+;
 %0A       

Fix some warnings found by scrutinizer