iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🐰

Sorting [String] in Swift Like macOS Finder List View

に公開
2024/02/29
1件

Swift Sorting

localizedStandardCompare(_:)

To achieve the same sort order as the Finder list, it is best to use localizedStandardCompare(_:) from NSString.
Note that the sorting result depends on the Locale settings.

https://developer.apple.com/documentation/foundation/nsstring/1409742-localizedstandardcompare

let sorted1 = objects.sorted {
	$0.localizedStandardCompare($1) == .orderedAscending
}

sorted(using:)

In Swift, you can also use SortComparator's sorted(using:) instead of NSString's localizedStandardCompare(_:). Provide String.Comparator.localizedStandard as the argument. Note that the sorting result depends on the Locale settings.
Available from iOS 15.0, macOS 12.0, tvOS 15.0, and watchOS 8.0.

https://developer.apple.com/documentation/foundation/sortcomparator/3802507-localizedstandard

// Since `.localizedStandard` depends on currentLocale,
// we forcefully override it for environments like Playgrounds.
// In a general runtime, you can leave it as the system setting.
extension NSLocale {
	@objc
	static let currentLocale = NSLocale(localeIdentifier: "ja_JP")
}

let sorted2 = objects.sorted(using: .localizedStandard)

sorted(using:) + SortDescriptor

If the objects being sorted have a complex structure, you can also use SortDescriptor with a KeyPath specification.

// @available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
// Since the default value for `comparator` is `.localizedStandard`, it can be omitted.
array.sorted(using: SortDescriptor(\.customProperty, comparator: .localizedStandard))

A Simple Experiment

Since an array of text starting with Kanji is easy to verify, I've prepared a list of anime titles for the experiment.
(The selection of titles has no particular meaning.)

let objects: [String] = [
	"天空の城ラピュタ",
	"蟲師",
	"アキバ冥途戦争",
	"夜は短し歩けよ乙女",
	"アルドノア・ゼロ",
	"約束のネバーランド",
	"冴えない彼女の育てかた",
	"86 Eighty Six",
	"異世界おじさん",
	"あそびあそばせ",
	"四畳半神話大系",
	"とある科学の超電磁砲",
	"サマータイムレンダ",
	"SPY×FAMILY",
	"かぐや様は告らせたい",
	"リコリス・リコイル",
	"陰の実力者になりたくて!",
	"Vivy -Fluorite Eye’s Song-",
	"天気の子",
	"ようこそ実力主義の教室へ",
	"進撃の巨人",
	"陰からマモル!",
	"ヴァイオレット・エヴァーガーデン",
	"この素晴らしい世界に祝福を!",
	"Dr. STONE",
]

Let's try sorting this in ascending order under several conditions.

Sort Result: Finder List View

For verification, I've posted the results of the Finder list view (Name, Ascending, Japanese environment). For now, let's consider this the ideal result.

Sort Result: localizedStandardCompare(_:)

With NSString's localizedStandardCompare(_:), the result was identical to the Finder list view, as described in the API documentation. The locale used was ja_JP.

86 Eighty Six
Dr. STONE
SPY×FAMILY
Vivy -Fluorite Eye’s Song-
アキバ冥途戦争
あそびあそばせ
アルドノア・ゼロ
ヴァイオレット・エヴァーガーデン
かぐや様は告らせたい
この素晴らしい世界に祝福を!
サマータイムレンダ
とある科学の超電磁砲
ようこそ実力主義の教室へ
リコリス・リコイル
異世界おじさん
陰からマモル!
陰の実力者になりたくて!
冴えない彼女の育てかた
四畳半神話大系
進撃の巨人
天気の子
天空の城ラピュタ
夜は短し歩けよ乙女
約束のネバーランド
蟲師

Sort Result: sorted(using: .localizedStandard)

To observe the differences resulting from locale settings, I verified three patterns.

ja_JP (Japanese, Japan)

When the locale is ja_JP, the result is identical to the Finder list view in a Japanese environment. The order is numbers, alphabet, Kana, and then Kanji.

Kana are sorted by sound without distinction between Hiragana and Katakana.

アキバ, あそび, アルドノア

Kanji appear to be sorted in the order of the Japanese syllabary (Gojuon) based on specific On-readings.

異 (I), 陰 (In)
四 (Shi), 進 (Shin), 天 (Ten)
夜 (Ya), 約 (Yaku)

86 Eighty Six
Dr. STONE
SPY×FAMILY
Vivy -Fluorite Eye’s Song-
アキバ冥途戦争
あそびあそばせ
アルドノア・ゼロ
ヴァイオレット・エヴァーガーデン
かぐや様は告らせたい
この素晴らしい世界に祝福を!
サマータイムレンダ
とある科学の超電磁砲
ようこそ実力主義の教室へ
リコリス・リコイル
異世界おじさん
陰からマモル!
陰の実力者になりたくて!
冴えない彼女の育てかた
四畳半神話大系
進撃の巨人
天気の子
天空の城ラピュタ
夜は短し歩けよ乙女
約束のネバーランド
蟲師

en_US (English, United States)

When the locale is en_US.
The order is numbers, alphabet, Kana, and then Kanji.

The order of Kanji appears to be based on Unicode order.

冴 U+51B4
四 U+56DB
夜 U+591C
天 U+5929
異 U+7570
約 U+7D04
蟲 U+87F2
進 U+9032
陰 U+9670

86 Eighty Six
Dr. STONE
SPY×FAMILY
Vivy -Fluorite Eye’s Song-
アキバ冥途戦争
あそびあそばせ
アルドノア・ゼロ
ヴァイオレット・エヴァーガーデン
かぐや様は告らせたい
この素晴らしい世界に祝福を!
サマータイムレンダ
とある科学の超電磁砲
ようこそ実力主義の教室へ
リコリス・リコイル
冴えない彼女の育てかた
四畳半神話大系
夜は短し歩けよ乙女
天気の子
天空の城ラピュタ
異世界おじさん
約束のネバーランド
蟲師
進撃の巨人
陰からマモル!
陰の実力者になりたくて!

zh_TW (Chinese, Taiwan)

When the locale is zh_TW (Taiwan).
The order is numbers, Kanji, alphabet, and then Kana.

I couldn't determine the exact order of the Kanji, but it likely follows a natural sequence based on the local reading.

86 Eighty Six
天気の子
天空の城ラピュタ
四畳半神話大系
冴えない彼女の育てかた
夜は短し歩けよ乙女
約束のネバーランド
異世界おじさん
陰からマモル!
陰の実力者になりたくて!
進撃の巨人
蟲師
Dr. STONE
SPY×FAMILY
Vivy -Fluorite Eye’s Song-
アキバ冥途戦争
あそびあそばせ
アルドノア・ゼロ
ヴァイオレット・エヴァーガーデン
かぐや様は告らせたい
この素晴らしい世界に祝福を!
サマータイムレンダ
とある科学の超電磁砲
ようこそ実力主義の教室へ
リコリス・リコイル

Discussion