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

6
  • 1
    This is not an Array at all, let alone a 3D one--it's just an object and the properties of the object don't have an order to them. Commented Jun 2, 2015 at 18:09
  • 1
    We are not a code translation service... Commented Jun 2, 2015 at 18:10
  • You can't do that... JavaScript objects are unordered. Commented Jun 2, 2015 at 18:10
  • You want inboxMessages to be an array ordered by the most recent item in each object. Ordered arrays do not have keys. Since each message contains its own id and thread_id, you will be able to identify each message object from its own keys and values. Is that right? Commented Jun 2, 2015 at 18:18
  • @JamesNewton yes.... Commented Jun 2, 2015 at 18:20

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

6 Comments

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..
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_ids and use that to find appropriate object in the nested output array.
I've added a couple of methods so that you can find the data for a given thread by the thread_id.
This can be done after the whole object converting in array??
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.
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.