JavaScript searches for strings in different order in the array and javascript searches for strings in different order.

Source: Internet
Author: User

JavaScript searches for strings in different order in the array and javascript searches for strings in different order.

Requirement: Find the array elements of a string in different order from a group of arrays. Suppose there is an array like this:

Copy codeThe Code is as follows:
['Abc', 'Hello', 'bdca', 'olleh', 'cadb', 'nba ', 'abn', 'abc']

The result is as follows:

Copy codeThe Code is as follows:
['Abcd', 'bdca', 'cadb']

The key point here is to determine whether a set of strings are only in different character order, as long as the entire key point is solved.

Method 1:
Copy codeThe Code is as follows:
Var stringClassify = function (arr ){
Var arrLength = arr. length,
Obj = {},
I = 0,
Num, item, name, firstItem, strLength;
 
For (; I <arrLength; I ++ ){
Item = arr [I];
StrLength = item. length;
Num = 0;
 
// Convert a single character to Unicode encoding
// Obtain and calculate the encoding.
For (j = 0; j <strLength; j ++ ){
Num + = item. charCodeAt (j );
}
 
If (! FirstItem ){
FirstItem = item;
Obj [num]. push (item );
}
// Check whether the first character of the string to be added is
// Appear in another string to avoid the following situations
// ['Ad', 'da', 'bc']
Else if (~ FirstItem. indexOf (item. charAt (0 ))){
Obj [num]. push (item );
}
}
 
For (name in obj ){
Console. log (obj [name]);
}
};

Method 1 traverses each character in a string and converts a single character to Unicode encoding. The encoding is obtained and calculated. The encoding of abcd and bdca is consistent. Finally, the encoding and the object key are used to save the encoded and consistent strings.

Method 1 It should be noted that the Unicode encoding of the string "ad" and "bc" is the same, and an additional judgment is required at this time, check whether the first character in any string has appeared in another string.

Method 2:

Copy codeThe Code is as follows:
Var stringClassify = function (){
Var arrLength = arr. length,
Obj = {},
I = 0,
Num, item, name, strArr, newStr;
 
For (; I <arrLength; I ++ ){
Item = arr [I];
 
StrArr = arr [I]. split ('');
StrArr. sort ();
NewStr = strArr. join ('');
 
If (! Obj [newStr]) {
Obj [newStr] = [];
}
 
Obj [newStr]. push (item );
}
 
For (name in obj ){
Console. log (obj [name]);
}
};

Method 2 is to convert the string into an array and then sort the array by sort. After using sort for abcd and bdca, it will become abcd, the sorted string is used as the object key to save the sorted string.

In fact, the principle of the two methods is to convert characters to Unicode encoding, but method 1 is an explicit conversion, and the sort sorting used in method 2 is implicitly converted.


Use JavaScript to sort string arrays.

/**
* Sort
@ Param opt: String Array to be sorted
*/
Function sortString (opt ){
If (! Opt) return;
Var result = [], nresult = [],
TempNum = 0, tempTNum = 0, nk = 0;
For (var I = 0; I <opt. length; I ++ ){
Var syt = opt [I], num = 0;
For (var j = 0; j <syt. length; j ++ ){
Var k = 0, tempnum = 0,
Str = syt. substr (j, j + 1 );
While (syt. indexOf (str, k )! =-1 ){
Tempnum ++;
K = syt. indexOf (str, k) + 1;
}

If (tempnum> num ){
Num = tempnum
}
}
Result [result. length] = num;
}
For (var n = 0; n <result. length; n ++ ){
TempTNum = result [n];
For (var m = 0; m <result. length; m ++ ){
If (tempTNum <result [m]) {
TempTNum = result [m];
Nk = m;
}
}
Nresult [nresult. length] = opt [nk];
Result [nk] =-1;
}
Return nresult;
}

In fact, this is an algorithm, but the above Code does not take efficiency into account, but only achieves the effect

Javascript array sorting

Reading the above section of your code is helpful to you.

Description
If no parameter is used when this method is called, the elements in the array are sorted alphabetically. More precise, the elements are sorted by character encoding. To achieve this, first convert the elements of the array into strings (if necessary) for comparison.

If you want to sort by other criteria, you need to provide a comparison function that compares two values and returns a number indicating the relative sequence of the two values. The comparison function should have two parameters, a and B. The returned values are as follows:

If a is less than B, a should appear before B in the sorted array, then a value smaller than 0 is returned.
If a is equal to B, 0 is returned.
If a is greater than B, a value greater than 0 is returned.
Reference: w3school

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.