I'dにこの配列をソートしたい:ソートアレイAS3
[ 'ラムジー'、 'セフォラ'、 '配列'、 'SER'、 'ユーザ']このよう
:
"Se"と入力すると、配列内の "se"(大文字または小文字)を含む文字列が最初に来るように配列がソートされます。
どうすればいいですか?
ありがとうございました。
I'dにこの配列をソートしたい:ソートアレイAS3
[ 'ラムジー'、 'セフォラ'、 '配列'、 'SER'、 'ユーザ']このよう
:
"Se"と入力すると、配列内の "se"(大文字または小文字)を含む文字列が最初に来るように配列がソートされます。
どうすればいいですか?
ありがとうございました。
技術的には彼らはすべての「SE」が含まれているので、あなたは、あなたが「SE」が含まれていないすべての要素を削除したい場合は、上filter()
を呼び出すことができます
:)をソートする必要はありません。あなたの配列は前に:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html#filter()
通常のようにアルファベット順に並べ替えます。新しい配列を作成するたびにfilter()
という独自のフィルタを作成することもできます。
オブジェクトを配列内に保持する場合は、独自のソートを実装する必要があります。このようなものはうまくいくはずです:
public function Test()
{
var a:Array = ['Ramsey', 'Sephora', 'seq', 'ser', 'user'];
trace(a); // Ramsey,Sephora,seq,ser,user
a.sort(this._sort);
trace(a); // Sephora,seq,ser,user,Ramsey
}
private function _sort(a:String, b:String):int
{
// if they're the same we don't care
if (a == b)
return 0;
// make them both lowercase
var aLower:String = a.toLowerCase();
var bLower:String = b.toLowerCase();
// see if they contain our string
var aIndex:int = aLower.indexOf("se");
var bIndex:int = bLower.indexOf("se");
// if one of them doesn't have it, set it afterwards
if (aIndex == -1 && bIndex != -1) // a doesn't contain our string
return 1; // b before a
else if (aIndex != -1 && bIndex == -1) // b doesn't contain our string
return -1; // a before b
else if (aIndex == -1 && bIndex == -1) // neither contain our string
return (aLower < bLower) ? -1 : 1; // sort them alphabetically
else
{
// they both have "se"
// if a has "se" before b, set it in front
// otherwise if they're in the same place, sort alphabetically, or on
// length or any other way we want
if (aIndex == bIndex)
return (aLower < bLower) ? -1 : 1;
return aIndex - bIndex;
}
}
var array:Array = ['Ramsey', 'Sephora', 'seq', 'ser', 'user'];
trace(array.sort(Array.CASEINSENSITIVE));