1

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

Community
  • 1
  • 1
kupendra
  • 1,002
  • 1
  • 15
  • 37

2 Answers2

1

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
James Newton
  • 6,623
  • 8
  • 49
  • 113
  • Its no work... i want `inboxMessages = {378552 : {0:{...},1:{...}}, 224199 : {0:{...},1:{...}} }}` But its give `inboxMessages = {0 : {0:{...},1:{...}}, 1 : {0:{...},1:{...}} }}` Its sorting perfect but `thread_id` is not there as key.. – kupendra Jun 03 '15 at 07:38
  • In JavaScript, objects (which use curly brackets {}) have no fixed order for their keys. You can *create* an object with the keys in a specific order, but you cannot be sure that they will be *stored* in that order. To preserve order, you need to use an array (which uses square brackets []). One solution would be to create an ordered array of `thread_id`s and use that to find appropriate object in the nested `output` array. – James Newton Jun 03 '15 at 08:16
  • I've added a couple of methods so that you can find the data for a given thread by the thread_id. – James Newton Jun 03 '15 at 08:42
  • This can be done after the whole object converting in array?? – kupendra Jun 03 '15 at 09:54
  • In this case, yes, because each message object contains all the information that is needed. In other cases, the object keys may not be duplicated anywhere, and so data would be lost when the arrays are created. In a case like this, you would need to generate the lookup array at the same time that you create the sorted array. – James Newton Jun 03 '15 at 11:52
  • You cannot count on your solution always working. Please see [this question and its answers](http://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order) for more details. – James Newton Jun 03 '15 at 13:07
0

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.

Community
  • 1
  • 1
kupendra
  • 1,002
  • 1
  • 15
  • 37