« 【A】sortedArrayUsingDescriptors: | トップページ | [Bash]ffmpegインストール appleScriptは少し直した »

[NSArray]ソート 2種

【A】sortedArrayUsingDescriptors:
複数のソート条件を指定できる 
https://quicktimer.cocolog-nifty.com/icefloe/2023/09/post-4c46eb.html

【B】sortedArrayUsingSelector:
特定の条件にあわせてソートできる 簡単
https://quicktimer.cocolog-nifty.com/icefloe/2023/09/post-dae2be.html


【A】sortedArrayUsingDescriptors:({Array})
複数のソート条件を指定できる
Descriptor
sortDescriptorWithKey:ascending:selector:
Sortdescriptorwithkey0021920x108072ppirg


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions
property refMe : a reference to current application



######【KEY】
(*
■ARRAYの値が 文字列や数値の場合
self
■ARRAYの値が NSURLなら
absoluteString
host
path
lastPathComponent 等が利用できます
■ARRAYの値が DICTなら
dictのKEYを利用できる
*)
##一般的なARRAYの場合は self 一択
set listForArray to {"東京", "神奈川", "千葉", "埼玉", "茨城", "群馬", "AA", "bb", "aa", "1234"} as list
set ocidArrayM to refMe's NSMutableArray's arrayWithArray:(listForArray)
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("self") ascending:(yes) selector:"localizedStandardCompare:"
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
log ocidSortedArray as list

##NSURLが格納されているArrayの場合
set ocidDirPathStr to refMe's NSString's stringWithString:("/System/Library/Templates/Data/Library/User Pictures/Instruments")
set ocidDirPath to ocidDirPathStr's stringByStandardizingPath()
set ocidDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidDirPath) isDirectory:true)
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidArrayM to appFileManager's contentsOfDirectoryAtURL:(ocidDirPathURL) includingPropertiesForKeys:({(refMe's NSURLPathKey)}) options:(refMe's NSDirectoryEnumerationSkipsHiddenFiles) |error|:(missing value)
##ファイル名(lastPathComponent)でソート
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("lastPathComponent") ascending:(yes) selector:"localizedStandardCompare:"
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
log ocidSortedArray as list

set listForArray to {{reg:"東京"}, {reg:"神奈川"}, {reg:"千葉"}, {reg:"埼玉"}, {reg:"茨城"}, {reg:"群馬"}} as list
set ocidArrayM to refMe's NSMutableArray's arrayWithArray:(listForArray)

set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("reg") ascending:(yes) selector:"localizedStandardCompare:"
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
log ocidSortedArray as list


set listForArray to {"東京", "神奈川", "千葉", "埼玉", "茨城", "群馬", "AA", "bb", "aa", "1234"} as list
set ocidArrayM to refMe's NSMutableArray's arrayWithArray:(listForArray)

######【ascending】
# TRUE YES 正順
# FALSE NO 逆順

set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("self") ascending:(yes) selector:"localizedStandardCompare:"
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
log ocidSortedArray as list
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("self") ascending:(no) selector:"localizedStandardCompare:"
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
log ocidSortedArray as list

######【selector】
(*
漢字が入るなら通常これ
localizedStandardCompare
英数小文字を『先』
compare
localizedCompare
英数大文字小文字判定しない
caseInsensitiveCompare
localizedCaseInsensitiveCompare
*)
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("self") ascending:(yes) selector:"localizedStandardCompare:"
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
log ocidSortedArray as list
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("self") ascending:(yes) selector:"compare:"
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
log ocidSortedArray as list
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("self") ascending:(yes) selector:"localizedCompare:"
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
log ocidSortedArray as list
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("self") ascending:(yes) selector:"caseInsensitiveCompare:"
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
log ocidSortedArray as list
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("self") ascending:(yes) selector:"localizedCaseInsensitiveCompare:"
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
log ocidSortedArray as list

【B】sortedArrayUsingSelector:
特定の条件にあわせてソートできる



【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions
property refMe : a reference to current application

set listForArray to {"東京", "神奈川", "千葉", "埼玉", "茨城", "群馬", "AA", "bb", "aa", "1234"} as list

set ocidArrayM to refMe's NSMutableArray's arrayWithArray:(listForArray)

#######漢字が入るなら通常これ
set ocidSortedArray to ocidArrayM's sortedArrayUsingSelector:("localizedStandardCompare:")
log ocidSortedArray as list
#######英数小文字を『先』なら
set ocidSortedArray to ocidArrayM's sortedArrayUsingSelector:("compare:")
log ocidSortedArray as list
set ocidSortedArray to ocidArrayM's sortedArrayUsingSelector:("localizedCompare:")
log ocidSortedArray as list
#######英数大文字小文字判定しない
set ocidSortedArray to ocidArrayM's sortedArrayUsingSelector:("caseInsensitiveCompare:")
log ocidSortedArray as list
set ocidSortedArray to ocidArrayM's sortedArrayUsingSelector:("localizedCaseInsensitiveCompare:")
log ocidSortedArray as list

|

« 【A】sortedArrayUsingDescriptors: | トップページ | [Bash]ffmpegインストール appleScriptは少し直した »

AppleScript Sort」カテゴリの記事