Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7531129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:05:50+00:00 2026-05-30T05:05:50+00:00

I’m no Javascript expert and I’m having problems trying to glue together the various

  • 0

I’m no Javascript expert and I’m having problems trying to glue together the various nuggets I find here and elsewhere regarding multi-dimensional arrays and sorting and wondered if someone could help me with a complete example?

I have managed to get to the point that I can populate a localStorage with data read in via Ajax.

The format of the rows is …

(msgXXX) (Key1:Value1|Key2:Value2|Key3:Value3|…etc)

where

(msgXXX) is the localStorage key; and

(Key1:Value1|Key2:Value2|Key3:Value3|…etc) is the single concatenated localStorage data string

What I want to be able to do is convert all this to a multi-dimensional array to which I can apply various sorts. For example, one of the Keys is called "Timestamp" and the value is an integer representing seconds since the Unix epoch. I would like to sort all rows based on this Timestamp value (in descending order – ie latest first). Right now the dataset is just over 600 rows.

I’m comfortable I can do the extraction and slicing and dicing to get the data out of the localStorage, but I’m not even sure what I’m aiming for with regards to populating an array and then setting up the sort.

Can someone point me in the right direction?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T05:05:51+00:00Added an answer on May 30, 2026 at 5:05 am

    You can go with something like this:

    function create(line) {
        var tokens = line.split("|");
        var obj = {};
        for (var i = 0; i < tokens.length; i++) {
            tokens[i] = tokens[i].split(":");
            obj[tokens[i][0]] = tokens[i][1];
        }
    
        return obj;
    }
    
    var arr = [];
    for (....) { // iterate over the input that each line is of key/value format
        arr.push(create(line));
    }
    
    function timestampSort(a, b) {
        if (a == b)
            return 0;
    
        return a.timestamp < b.timestamp ? -1 : 1;
    }
    
    // to sort by timestamp
    arr.sort(timestampSort);
    

    This code creates an object per key/value line, in the format you gave. The object will have the keys as attributes. All of those objects are being pushed into an array, which is then being sorted by passing a compare function to the native sort method of array.
    You can of course make as many compare functions as you want, each comparing by a different attribute/criteria.

    You can read more about the sort method here: http://www.w3schools.com/jsref/jsref_sort.asp

    EDIT

    The sort method both changes the array itself and returns the array, so doing something like:

    console.log(arr.sort(timestampSort));
    

    Will both change the actual array and return it, and so the console.log will print it.
    If you don’t want to change the original array and have a copy of it that will get sorted you can:

    var arr2 = arr.slice();
    arr2.sort(timestampSort);
    

    As for the keys in the array, what I wrote was intended to work only with this part of the line: Key1:Value1|Key2:Value2|Key3:Value3|…etc
    So, to add support for the entire format, here’s the modification:

    function create(line) {
        var parts = line.match(/^\(msg(\d+)\) \((.+)\)$/);
        var tokens = parts[2].split("|");
        var obj = { msgID: parts[1] };
        for (var i = 0; i < tokens.length; i++) {
            tokens[i] = tokens[i].split(":");
            obj[tokens[i][0]] = tokens[i][1];
        }
        return obj;
    }
    

    If you apply this to the example you gave you’ll get this:

    arr is: [{
        msgID: XXX,
        Key1: Value1,
        Key2: Value2,
        Key3: Value3
    }]
    

    Hope this clears things for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I used javascript for loading a picture on my website depending on which small
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I need to clean up various Word 'smart' characters in user input, including but
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.