0

I have array as following, I need it to apply natural sort on this, on only text field.

    var data = [{
        "text": "1001",
        "value": "212121"
    }, {
        "text": "1002",
        "value": "32435"
    }, {
        "text": "A101",
        "value": "324124324"
    }, {
        "text": "A12",
        "value": "567y54645"
    }, {
        "text": "A123",
        "value": "534534"
    }, {
        "text": "A21",
        "value": "34534534"
    }, {
        "text": "A210",
        "value": "5345345"
    }, {
        "text": "A33",
        "value": "234234234"
    }, 
        "text": "B2",
        "value": "4234234"
    }, {
        "text": "D10000",
        "value": "34234234"
    }, {
        "text": "EZH43NUT8SD",
        "value": "534534534"
    }, {
        "text": "H287",
        "value": "43435345"
    }, {
        "text": "Pkg test",
        "value": "5345345"
    }]

I have tried following things so far :

http://jsfiddle.net/wE7H2/3/

AngularJS - Sorting ng-repeat on string with numbers in them

4
  • 4
    What do you mean by natural sort in this context? Commented Aug 24, 2017 at 6:44
  • Do you mean lexicographical sort ? Commented Aug 24, 2017 at 6:45
  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. See: How to create a Minimal, Complete, and Verifiable example." Commented Aug 24, 2017 at 6:46
  • Thanks for your comments, I have marked correct answer I needed. Commented Aug 24, 2017 at 7:03

1 Answer 1

2

You could use String#localeCompare with options

sensitivity

Which differences in the strings should lead to non-zero result values. Possible values are:

  • "base": Only strings that differ in base letters compare as unequal. Examples: a ≠ b, a = á, a = A.
  • "accent": Only strings that differ in base letters or accents and other diacritic marks compare as unequal. Examples: a ≠ b, a ≠ á, a = A.
  • "case": Only strings that differ in base letters or case compare as unequal. Examples: a ≠ b, a = á, a ≠ A.
  • "variant": Strings that differ in base letters, accents and other diacritic marks, or case compare as unequal. Other differences may also be taken into consideration. Examples: a ≠ b, a ≠ á, a ≠ A.

The default is "variant" for usage "sort"; it's locale dependent for usage "search".

numeric

Whether numeric collation should be used, such that "1" < "2" < "10". Possible values are true and false; the default is false. This option can be set through an options property or through a Unicode extension key; if both are provided, the options property takes precedence. Implementations are not required to support this property.

var data = [{ text: "1001", value: "212121" }, { text: "1002", value: "32435" }, { text: "A101", value: "324124324" }, { text: "A12", value: "567y54645" }, { text: "A123", value: "534534" }, { text: "A21", value: "34534534" }, { text: "A210", value: "5345345" }, { text: "A33", value: "234234234" }, { text: "B2", value: "4234234" }, { text: "D10000", value: "34234234" }, { text: "EZH43NUT8SD", value: "534534534" }, { text: "H287", value: "43435345" }, { text: "Pkg test", value: "5345345" }, { text: "RRG46AXC3PO", value: "3434354" }, { text: "yoyo", value: "534534534" }];

data.sort(function (a,b) {
    return a.text.localeCompare(b.text, undefined, { numeric: true, sensitivity: 'base' });
});

console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly what I needed, Thank you so much. :)

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.