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

  • Home
  • SEARCH
  • 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 8077263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:26:12+00:00 2026-06-05T15:26:12+00:00

I have an array with the following values asd sdf dsdf 1sadf *sdf !sdf

  • 0

I have an array with the following values

asd sdf dsdf 1sadf *sdf !sdf @asdf _asd .sadf (sadf )sadf #sadf 
^asdf &asdf %asdf -sadf =sadf +sadf -sdf

and i want to sort it in javascript in the following way in to three parts.

  1. word starting from special character
  2. word starting from digit
  3. word starting from alphabets.

So this should be the sequence of the sorted array.

EDIT:
Here’s a function that I’ve been experimenting with:

function naturalSort(a, b) {
   a = a.path.toLowerCase();
   b = b.path.toLowerCase();
   var re = /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi,
  sre = /(^[ ]*|[ ]*|[_]*$)/g,
  dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/,
  hre = /^0x[0-9a-f]+$/i,
  ore = /^0/,
   // convert all to strings and trim()
  x = a.toString().replace(sre, '') || '',
  y = b.toString().replace(sre, '') || '',
   // chunk/tokenize
  xN = x.replace(re, '\0$1\0').replace(/\0$/, '').replace(/^\0/, '').split('\0'),
  yN = y.replace(re, '\0$1\0').replace(/\0$/, '').replace(/^\0/, '').split('\0'),
   // numeric, hex or date detection
  xD = parseInt(x.match(hre)) || (xN.length != 1 && x.match(dre) && Date.parse(x)),
  yD = parseInt(y.match(hre)) || xD && y.match(dre) && Date.parse(y) || null;
   // first try and sort Hex codes or Dates
   if (yD)
    if (xD < yD) return -1;
    else if (xD > yD) return 1;
   // natural sorting through split numeric strings and default strings
   for (var cLoc = 0, numS = Math.max(xN.length, yN.length); cLoc < numS; cLoc++) {
    // find floats not starting with '0', string or 0 if not defined (Clint Priest)
    oFxNcL = !(xN[cLoc] || '').match(ore) && parseFloat(xN[cLoc]) || xN[cLoc] || 0;
    oFyNcL = !(yN[cLoc] || '').match(ore) && parseFloat(yN[cLoc]) || yN[cLoc] || 0;
    // handle numeric vs string comparison - number < string - (Kyle Adams)
    if (isNaN(oFxNcL) !== isNaN(oFyNcL)) return (isNaN(oFxNcL)) ? -1 : 1;
    // rely on string comparison if different types - i.e. '02' < 2 != '02' < '2'
    else if (typeof oFxNcL !== typeof oFyNcL) {
     oFxNcL += '';
     oFyNcL += '';
    }
    if (oFxNcL <= oFyNcL) return -1;
    if (oFxNcL >= oFyNcL) return 1;
   }
   return 0;
  }
  • 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-05T15:26:13+00:00Added an answer on June 5, 2026 at 3:26 pm

    To be honest, I have no idea what your posted function does … at all.

    The following approach compares strings on their first character, using positional occurrence. Strings with the same first character are sorted regularly.

    Btw, didn’t test for empty strings.

    function MySort(alphabet)
    {
        return function(a, b) {
            var index_a = alphabet.indexOf(a[0]),
            index_b = alphabet.indexOf(b[0]);
    
            if (index_a === index_b) {
                // same first character, sort regular
                if (a < b) {
                    return -1;
                } else if (a > b) {
                    return 1;
                }
                return 0;
            } else {
                return index_a - index_b;
            }
        }
    }
    
    var items = ['asd','sdf', 'dsdf', '1sadf', '*sdf', '!sdf', '@asdf', '_asd', '.sadf', '(sadf', ')sadf', '#sadf', '^asdf', '&asdf', '%asdf', '-sadf', '=sadf', '+sadf', '-sdf', 'sef'],
    sorter = MySort('*!@_.()#^&%-=+01234567989abcdefghijklmnopqrstuvwxyz');
    
    console.log(items.sort(sorter));
    

    Output:

    ["*sdf", "!sdf", "@asdf", "_asd", ".sadf", "(sadf", ")sadf", "#sadf", "^asdf", 
     "&asdf", "%asdf", "-sadf", "-sdf", "=sadf", "+sadf", "1sadf", 
     "asd", "dsdf", "sdf", "sef"]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array which contains duplicate values. I want to sort the array
I have the following values: $attached_products = 1,4,3; I want to make an array
Lets say I have an array numbers that contains the following values: int numbers
I have an array called: $fragment = array($fragment); Which has the following three values:
I'm sorry about the naive question. I have the following: public Array Values {
I have an array and I set to this array random values. I want
Let's say I have an array with the following values: 0.7523262 0.9232192 1.5824928 5.2362123
I have the following values from a database call that I want to apply
I have a column array with the following values in my sheet: 11, 15,
I have a String array with the following values: dim strArray() as string dim

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.