简体   繁体   中英

I want to sort 3 dimensional array in javascript

I recently post this question for PHP but now i want it for javascript

My array is following :

var inboxMessages = {
    105775: { //index is thread_id
        0: {
            'id': 85,
            'thread_id': 105775,
            'message': "hello",
            'created_at': "May 20, 2015",
            'datetime': 1432118191,
            'sender_id': 13,
        },
        1: {
            'id': 70,
            'thread_id': 105775,
            'message': "hii",
            'created_at': "May 19, 2015",
            'datetime': 1432021227,
            'sender_id': 13,
        }
    },
    224199: { //index is thread_id
        0: {
            'id': 88,
            'thread_id': 224199,
            'message': "yessss...",
            'created_at': "May 20, 2015",
            'datetime': 1432306513,
            'sender_id': 14,
        },
        1: {
            'id': 75,
            'thread_id': 224199,
            'message': "hellowwww...",
            'created_at': "May 19, 2015",
            'datetime': 1432021227,
            'sender_id': 14,
        }
    },
    107917: { //index is thread_id
        0: {
            'id': 56,
            'thread_id': 107917,
            'message': "how r u??",
            'created_at': "May 16, 2015",
            'datetime': 1431792155,
            'sender_id': 14,
        },
        1: {
            'id': 30,
            'thread_id': 107917,
            'message': "hi.. i m fine.",
            'created_at': "May 6, 2015",
            'datetime': 1430920006,
            'sender_id': 14,
        },
        2: {
            'id': 30,
            'thread_id': 107917,
            'message': "hi!!!!..how r u??",
            'created_at': "May 6, 2015",
            'datetime': 1430920006,
            'sender_id': 14,
        }
    },
    378552: { //index is thread_id
        0: {
            'id': 108,
            'thread_id': 378552,
            'message': "hi",
            'created_at': "May 29, 2015",
            'datetime': 1432906923,
            'sender_id': 14,
        },
        1: {
            'id': 107,
            'thread_id': 378552,
            'message': "hi.",
            'created_at': "May 29, 2015",
            'datetime': 1432903194,
            'sender_id': 14,
        }
    }

};

So now i need Output like this :

var inboxMessages = {
    378552: {//index is thread_id
        0: {
            'id': 108,
            'thread_id': 378552,
            'message': "hi",
            'created_at': "May 29, 2015",
            'datetime': 1432906923,
            'sender_id': 14,
        },
        1: {
            'id': 107,
            'thread_id': 378552,
            'message': "hi.",
            'created_at': "May 29, 2015",
            'datetime': 1432903194,
            'sender_id': 14,
        }
    },
    224199: {//index is thread_id
        0: {
            'id': 88,
            'thread_id': 224199,
            'message': "yessss...",
            'created_at': "May 20, 2015",
            'datetime': 1432306513,
            'sender_id': 14,
        },
        1: {//index is thread_id
            'id': 75,
            'thread_id': 224199,
            'message': "hellowwww...",
            'created_at': "May 19, 2015",
            'datetime': 1432021227,
            'sender_id': 14,
        }
    },
    105775: {//index is thread_id
        0: {
            'id': 85,
            'thread_id': 105775,
            'message': "hello",
            'created_at': "May 20, 2015",
            'datetime': 1432118191,
            'sender_id': 13,
        },
        1: {
            'id': 70,
            'thread_id': 105775,
            'message': "hii",
            'created_at': "May 19, 2015",
            'datetime': 1432021227,
            'sender_id': 13,
        }
    },
    107917: {//index is thread_id
        0: {
            'id': 56,
            'thread_id': 107917,
            'message': "how r u??",
            'created_at': "May 16, 2015",
            'datetime': 1431792155,
            'sender_id': 14,
        },
        1: {
            'id': 30,
            'thread_id': 107917,
            'message': "hi.. i m fine.",
            'created_at': "May 6, 2015",
            'datetime': 1430920006,
            'sender_id': 14,
        },
        2: {
            'id': 30,
            'thread_id': 107917,
            'message': "hi!!!!..how r u??",
            'created_at': "May 6, 2015",
            'datetime': 1430920006,
            'sender_id': 14,
        }
    },
};

I don't know to how to sort this in JS.

I need recent datetime of thread's array is top on array

Does this do what you want?

//var inboxMessages = {... your input ...};

function convertToOrderedArrays(inboxMessages) {
  var output = [];
  var thread_ids = Object.keys(inboxMessages);
  var threadObject, threadArray, keys;

  for (var ii=0, thread_id; thread_id=thread_ids[ii]; ii++) {
    threadObject = inboxMessages[thread_id];
    keys = Object.keys(threadObject);
    threadArray = [];
    output.push(threadArray);

   for (var jj=0, key; key=keys[jj]; jj++) {
    threadArray.push(threadObject[key]);   
   }

    threadArray.sort(function (a, b) {
      return (b.datetime - a.datetime);
    });

  }

  output.sort(function (a, b) {
    return(b[0].datetime - a[0].datetime);
  });

  return output;
}


function getThreadIdArray(threadArrays) {
  var thread_id_array = [];

  for (var ii=0, thread_array; thread_array=threadArrays[ii]; ii++) {
    thread_id = thread_array[0].thread_id;
    thread_id_array.push(thread_id);
  }

  return thread_id_array;
}

function findThreadArray(thread_id, thread_id_array, threadArrays) {
  var index = thread_id_array.indexOf(thread_id);
  var threadArray;

  if (index < 0) {
    // no matching thread was found
  } else {
    threadArray = threadArrays[index];
  }

  return threadArray;
}


var output = convertToOrderedArrays(inboxMessages);
var thread_id_array = getThreadIdArray(output);
var mostRecentThread = thread_id_array[0];
var mostRecentMessage = output[0][0];
var thread105775 = findThreadArray(105775, thread_id_array, output);

console.log(output);
// sorted array of sorted arrays
console.log(thread_id_array);
// [378552, 224199, 105775, 107917]
console.log(mostRecentThread);
// 378552
console.log(mostRecentMessage);
/* {
  created_at: "May 29, 2015"
, datetime: 1432906923
, id: 108
, message: "hi"
, sender_id: 14
, thread_id: 378552
} */
console.log(thread105775);
// sorted array

This array is already sorting from server script of PHP But when i get it from ajax and convert JSON to array then ONLY chrome web browser sort it in numeric thread_id in ascending order.

So now I'm add _ before thread_id from server side to key as _378552 is now string so no sort by CHROME.

And after all i remove _ from thread_id while accessing it.. so my problem solved. This is only problem in chrome browser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM