Maneesh Pal
Maneesh Pal

Reputation: 101

sorting in Alphanumeric in javascript or jQuery

I have data in select field like this

72A(6) , 72(A)(6A) , 78(1) , 72AB(1) , 79 , 80QQB , 80RRB , 80TTA , 80U , 80CCC , 80CCD , 80CCE , 80CCF , 80CCG , 80D , 80DD , 80DDB , 80E , 80EE , 80G , 92C(4) , 80G(2)(c ) , 80G(5) , 80GG , 80C , 80GGA , 80GGB , 80GGC , 80-IA , 80-IA(4) , 80-IA(4)(ii) , 80-IA(4)(iii) , 80-IA(4)(iv) , 80-IA(4)(v) , 80-IA(5) , 80-IA(4)(i) , 80-IA(12) , 80-IAB , 80-IB , 80-IB(2) , 80-IB(3) , 80-IB(4) , 80-IB(5) , 80-IB(11) , 80-IB(7) , 80-IB(7A) , 80-IB(7B) , 80-IB(8A) , 80-IB(9) , 80-IB(10) , 80-IB(11A) , 80-IB(11B) , 80-IB(11C) , 80-IC , 80-ID , 80-ID(3) , 80-IE , 80JJA , 80LA , 80JJAA , 80P , 56(2)(vii) , 184(2) , 184 , 144 , 184(5) , 185 , 47(xiiib) , 32(1) , 35DDA(4A) , 43(1) , 43(6) , 47A(4) , 49(1) , 49(2AAA) , 72A(6A) , 115JAA , 167C , 36 , 37 , 40(b) , 40A(2) , 187(2) , 115JC , 115JD , 115JEE , 115JE , 115JF , 187 , 170 , 188 , 189 , 188A , 2(31) , 111A , 112 , 86 , 80P(2)(a)(iii) , 11 , 11(1)(d) , 12 , 11(1) , 11(2) , 11(5) , 11(3) , 11(3A) , 115BBC , 11(1A) , 2(24)(vi) , 11(4) , 11(4A) , 12A , 12AA , 2(15) , 13(8) , 13 , 13(3) , 13(2) , 164 , 161 , 143(3) , 115JB , 115JB(5) , 2(26) , 115-O(6) , 115-O(3) , 115-O(4) , 115P , 115Q , 10(34) , 2(22)(e) , 2(18) , 6(3) , 115T , 115R , 115TA , 115TA(2) , 115TB , 115TC , 115U , 115QA(3) , 115QA(4) , 115QA(5) , 115QB , 115QC , 115QA , 115R(3) , 115VS , 115VE , 115VX , 115V-I (1) , 115V-I (2) , 115V-I (5) , 115V-I (6) , 115V-I (7) , 115VA , 115V-I (8) , 115VT , 115VT(2) , 115VT(3) , 115VT(4) , 115VT(5) , 115VL , 115VK , 115VN , 115VY , 115VB , 115VC , 115VZ , 115VO , 115VZA , 115VZB , 115VZC , 115VD , 115VP , 115VQ(1) , 115VQ(2) , 194LB , 194LC , 194LD , 9(1) , 115A , 2(30) , 115AB , 115AC , 115AD , 115BBA , 115E , 115(C)(b) , 115(C)(f) , 115D , 115F , 115G , 115H , 115-I , 115A(1)(a) , 115JG , 163 , 139(1) , 145(3) , 145 , 144A , 144C , 144C(15) , 144BA , 144BA(2) , 144BA(6) , 144C(1) , 18B , 18BA , 30 , 31 , 32 , 33 , 35GA , 35A , 35B , 35C , 35D , 35E , 35EE , 35EEE , 35FG , 34A , 35H , 35HA , 3 , 2(ea)(ii) , 2(ea)(iii) , 2(ea)(iv) , 2(ea)(v) , 2(ea)(vi) , 4(1)(a) , 4 , 4(1) , 4(1)(6) , 5(i) , 5(ii) , 5(iii) , 45 , 5(iv) , 5(v) , 5(vi) , 2(q) , 5 , 44C , 44D , 2(c ) , 7 , 7(2) , 7(1) , 14(1) , 14(2) , 15A , 60 , 273A(2) , 174A , 175 , 64(1)(iv) , 80B , 2(23) , 234B(3) , 90(2A) , 6 , 6(6)(a) , 194E , 234D , 6(6)(b) , 115BB , 5(2) , 56(2)(viib) , 115R(4) , 20 , 139 , 14 , 10(10B) , 115VF , 115VG , 17(3)(i) , 17(3)(iii) , 28(ii) , 28(va) , 2(24)(iia) , 56(2)(viia) , 2(25A) , 115BBD , 49(2) , 49(2A) , 49(2AA) , 49(2AB) , 49(2E) , 55(2)(b) , 2(22B) , 55(2)(a) , 55(2)(aa) , 48 , 55(2) , 55(2)(ab) , 2(22)(d) , 49(4) , 50C(2) , 51 , 55(1)(b) , 49 , 28 , 45(2) , 115ACA , 112(1) , 2(48) , 2(47) , 2(42A) , 45(1A) , 45(3) , 2(14)(ii) , 45(4) , 45(1) , 45(5) , 45(5)(b) , 45(5)(c ) , 155(16) , 145A(b) , 46 , 46(2) , 55(2)(b)(iii) , 48(ii) , 55 , 50A , 32(1)(i) , 41(2) , 50C , 2(14)(iii) , 45(6) , 46A , 45(2A) , 47A(2) , 47(xiiia) , 49(3) , 47(iv) , 47(v) , 115I , 55A , 10(41) , 54 , 54F , 2(42B) , 2(29B) , 2(29A) , 54EC , 47(xvi) , 2(47)(v) , 2(47)(i) , 2(47)(ii) , 2(47)(iii) , 2(47)(iv) , 47(5) , 2(47)(vi) , 47(xv) , 56 , 50D , 36(1)(iii) , 49(1)(i) , 55(3) , 56(1) , 2(22)(b) , 2(22)(a) , 56(2) , 94(1) , 94(6) , 94(8) , 57 , 56(2)(iii) , 57(ii) , 57(iii) , 57(iia) , 37(1) , 58 , 56(2)(vii)(b) , 56(vii) , 56(viia) , 59 , 43A , 43 , 32(1)(iii) , 38(2) , 32(1)(iia) , 32(2) , 32AC , 33AB , 33AB(4) , 33AB(3) , 33AB(8) , 33ABA , 33ABA(4) , 43(4)(i) , 35(1)(i) , 43(4)(ii) , 35(2)(i) , 35(2B) , 43(4)(iii) , 41(3) , 73(3) , 35(1)(iii) , 35(1)(iia) , 35(2AA) , 35(2AB) , 35(3) , 35ABB , 35ABB(2) , 35AC , 35AC(2) , 35AC(4) , 35AC(5) , 35CCA , 35CCC , 35CCD , 35D(2) , 35D(3) , 34D(3) , 35DDA , 36(1)(i) , 36(1)(ia) , 36(1)(ib) , 36(1)(ii) , 36(1)(iiia) , 36(1)(iv) , 43B , 36(1)(iva) , 36(1)(v) , 36(1)(vi) , 41(4) , 36(1)(viia) , 36(1)(viii) , 36(1)(xii) , 36(1)(xv) , 36(1)(xiv) , 36(1)(xvi) , 35AB , 37(2B) , 40(a)(ia) 

but I want data like this:

1,2,2(24),2(35),2AB(1),2-IT(i),2(TT)(iii),3,4,6,7(1)(iii)

I also use natural sort of text and numbers, JavaScript this type all solution but it's not working.

How can I solve my problem?

Upvotes: 0

Views: 538

Answers (3)

Mask5323
Mask5323

Reputation: 158

you can use this

var uniqueName =  [72A(6) , 72(A)(6A) , 78(1) , 72AB(1) , 79 , 80QQB , 80RRB , 80TTA , 80U , 80CCC , 80CCD , 80CCE , 80CCF , 80CCG , 80D , 80DD , 80DDB , 80E , 80EE , 80G , 92C(4) , 80G(2)(c ) , 80G(5) , 80GG , 80C , 80GGA , 80GGB , 80GGC , 80-IA , 80-IA(4) , 80-IA(4)(ii) , 80-IA(4)(iii) , 80-IA(4)(iv) , 80-IA(4)(v) , 80-IA(5) , 80-IA(4)(i) , 80-IA(12) , 80-IAB , 80-IB , 80-IB(2) , 80-IB(3) , 80-IB(4) , 80-IB(5) , 80-IB(11) , 80-IB(7) , 80-IB(7A) , 80-IB(7B) , 80-IB(8A) , 80-IB(9) , 80-IB(10) , 80-IB(11A) , 80-IB(11B) , 80-IB(11C) , 80-IC , 80-ID , 80-ID(3) , 80-IE , 80JJA , 80LA , 80JJAA , 80P , 56(2)(vii) , 184(2) , 184 , 144 , 184(5) , 185 , 47(xiiib) , 32(1) , 35DDA(4A) , 43(1) , 43(6) , 47A(4) , 49(1) , 49(2AAA) , 72A(6A) , 115JAA , 167C , 36 , 37 , 40(b) , 40A(2) , 187(2) , 115JC , 115JD , 115JEE , 1]
  uniqueName.sort(sortAlphaNum);


var reA = /[^a-zA-Z]/g;
var reN = /[^0-9()]/g;
var resN = /[^0-9]/g;
function sortAlphaNum(a,b) {
        var aN = parseInt(a.replace(reN, ""), 10);
        var bN = parseInt(b.replace(reN, ""), 10);
        var a1 = a.indexOf('(');
        var a2 = a.indexOf(')');
        var b1 = b.indexOf('(');
        var b2 = b.indexOf(')');
        if(aN==bN){
            if(a1>-1 && a2>-1 && b1>-1 && b2>-1){
                var str1 = a.substring(a1+1,a2);
                var str2 = b.substring(b1+1,b2);
                var aN1 = parseInt(str1.replace(resN, ""), 10);
                var bN1 = parseInt(str2.replace(resN, ""), 10);
                if(aN1>bN1){
                    return 1;
                }else if(aN1<bN1){
                    return -1;
                }else{
                    var aN2 = str1.replace(reA, "");
                    var bN2 = str2.replace(reA, "");
                    if(aN2>bN2){
                        return 1;
                    }else if(aN2<bN2){
                        return -1;
                    }else{
                        return a === b ? 0 : a > b ? 1 : -1;

                    }

                }
            }
        }
        return aN === bN ? 0 : aN > bN ? 1 : -1;

}

Upvotes: 1

Begisen
Begisen

Reputation: 26

you can do as follows: first,the first word of old string is as the first word of the new string;then compare between the second word of the old string and the first word of the new string so that you will know which one should be the first of the new string(by the way,you should compare the first letter of them,if the same,compare the second letter.just do like this rule);then compare between the third word of the old string and every word of the new string so that you will know where the third word should be in the new string……,do like this util the last word of the old string,finally,the sorting of the new string is what you want.

Upvotes: 0

mehmetakifalp
mehmetakifalp

Reputation: 455

you can use jQuery sort()

jQuery;

 $("#jqSort").html($("#jqSort option").sort(function (a, b) {
     return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
 }))

JSFIDDLE : http://jsfiddle.net/mehmetakifalp/3bAuy/

Upvotes: 0

Related Questions