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 8870893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:50:56+00:00 2026-06-14T17:50:56+00:00

I have a requirement for the project I’m working on right now which is

  • 0

I have a requirement for the project I’m working on right now which is proving a bit tricky for me.

Basically I have to sort an array of items based on the Text property of those items:

Here are my items:

var answers = [],
    answer1 = { Id: 1, Text: '3-4 weeks ago' },
    answer2 = { Id: 2, Text: '1-2 weeks ago' },
    answer3 = { Id: 3, Text: '7-8 weeks ago' },
    answer4 = { Id: 4, Text: '5-6 weeks ago' },
    answer5 = { Id: 5, Text: '1-2 days ago' },
    answer6 = { Id: 6, Text: 'More than 1 month ago' };

answers.push(answer1);
answers.push(answer2);
answers.push(answer3);
answers.push(answer4);
answers.push(answer5);
answers.push(answer6);

I need to analyse the Text property of each item so that, after the sorting, the array looks like this:

answers[0] = { Id: 6, Text: 'More than 1 month ago' }
answers[1] = { Id: 3, Text: '7-8 weeks ago' }
answers[2] = { Id: 4, Text: '5-6 weeks ago' }
answers[3] = { Id: 1, Text: '3-4 weeks ago' }
answers[4] = { Id: 2, Text: '1-2 weeks ago' }
answers[5] = { Id: 5, Text: '1-2 days ago' }

The logic is that, the furthest away the date, the more high priority it is, so it should appear first in the array. So “1-2 days” is less of a priority then “7-8 weeks”.

So the logic is that, I need to extract the number values, and then the units (e.g. days, weeks) and somehow sort the array based on those details.

Quite honestly I’m finding it very difficult to come up with a solution, and I’d appreciate any help.

Edit:
Regards the data I’m working with, it’s coming back from a web service, and I don’t have the ability to change the text. So I have to work with it as is.

  • 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-06-14T17:50:57+00:00Added an answer on June 14, 2026 at 5:50 pm

    You can use a custom sort function that parses the values out of the text and compares them numerically as described here.

    var answers = [
        { Id: 1, Text: '3-4 weeks ago' },
        { Id: 2, Text: '1-2 weeks ago' },
        { Id: 3, Text: '7-8 weeks ago' },
        { Id: 4, Text: '5-6 weeks ago' },
        { Id: 5, Text: '1-2 days ago' },
        { Id: 6, Text: 'More than 1 month ago' }
    ];
    
    answers.sort(function(a, b) {
        function getVal(item) {
            var val;
            // find first number
            var match = item.Text.match(/\d+/);
            if (match) {
                val = parseInt(match[0], 10);
            } else {
                val = 10000;   // set to very high number
            }
            if (item.Text.indexOf("More than") !== -1) {
                ++val;
            }
            if (item.Text.indexOf("week") !== -1) {
                val *= 7;
            } else if (item.Text.indexOf("month") !== -1) {
                val *= 30;
            }
            return(val);
        }
        return(getVal(b) - getVal(a));
    });
    

    Working demo: http://jsfiddle.net/jfriend00/cf3D7/

    You can obviously fine tune the getVal() function to support whatever parsing logic you want.

    If your array is large, you can make it perform better by precomputing the sort index and storing it in each object so the custom sort function just directly compares two numbers rather than recalculates it every time. But, arrays less than 100 items that’s probably irrelevant.

    Also, it is unclear how you want to sort “More than 1 month ago” and “7-8 weeks”. My current algorithm treats “More than 1 month ago” as 1 month, but you can tweak the parsing logic if you have a particular rule in mind. Once you have the custom sort function, the key to your sort logic is all in the implementation of the getVal() function which you can tweak to support whatever syntax you want to support.

    Here’s a version that precomputes the sortKey and thus performs a lot better for large arrays:

    (function() {
        function getVal(item) {
            var val;
            // find first number
            var match = item.Text.match(/\d+/);
            if (match) {
                val = parseInt(match[0], 10);
            } else {
                val = 10000;   // set to very high number
            }
            if (item.Text.indexOf("More than") !== -1) {
                ++val;
            }
            if (item.Text.indexOf("week") !== -1) {
                val *= 7;
            } else if (item.Text.indexOf("month") !== -1) {
                val *= 30;
            }
            return(val);
        }
    
        for (var i = 0, len = answers.length; i < len; i++) {
            answers[i].sortKey = getVal(answers[i]);
        }
        answers.sort(function(a, b) {
            return(b.sortKey - a.sortKey);
        });
    })()
    

    P.S. Note the much more efficient array declaration syntax I used in the jsFiddle.

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

Sidebar

Related Questions

On the project that I'm currently working I have a requirement to run a
at the project I am working on, we have a requirement to have a
My project requires Java 1.6 for compilation and running. Now I have a requirement
As part of a project I'm working on we have a requirement to support
I have a requirement on my current project (a Flex app which will be
I have a project requirement which requires android to be always powered on with
In our project we have requirement that, after receiving sms message from third party
In our project we have a requirement that when a set of records are
For a project we have a requirement to create an interfacedefinition that will return
In a web project using jsp, I have following requirement Upload a file (say

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.