第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何按屬性值對(duì)自定義對(duì)象數(shù)組進(jìn)行排序

如何按屬性值對(duì)自定義對(duì)象數(shù)組進(jìn)行排序

幕布斯6054654 2019-06-19 10:50:04
如何按屬性值對(duì)自定義對(duì)象數(shù)組進(jìn)行排序假設(shè)我們有一個(gè)名為ImageFile的自定義類,這個(gè)類包含兩個(gè)屬性。class imageFile  {     var fileName = String()     var fileID = Int()}其中很多存儲(chǔ)在Array中var images : Array = []var aImage = imageFile()aImage.fileName = "image1.png"aImage.fileID = 101images.append(aImage)aImage = imageFile() aImage.fileName = "image1.png"aImage.fileID = 202images.append(aImage)問題是:如何根據(jù)“文件ID”ASC或DESC對(duì)圖像數(shù)組進(jìn)行排序?
查看完整描述

3 回答

?
BIG陽

TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊

首先,將Array聲明為類型化數(shù)組,以便在迭代時(shí)調(diào)用方法:

var images : [imageFile] = []

然后你可以簡(jiǎn)單地做:

SWIFT 2

images.sorted({ $0.fileID > $1.fileID })

SWIFT 3和SWIFT 4和SWIFT 5

images.sorted(by: { $0.fileID > $1.fileID })

上面的例子給出了DESC排序順序


查看完整回答
反對(duì) 回復(fù) 2019-06-19
?
吃雞游戲

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊

這是利用尾隨關(guān)閉:

images.sorted { $0.fileID < $1.fileID }

在你使用的地方<>分別取決于ASC或DESC。如果要修改images列陣,然后使用以下內(nèi)容:

images.sort { $0.fileID < $1.fileID }

如果您要重復(fù)這樣做,并且更愿意定義一個(gè)函數(shù),一種方法是:

func sorterForFileIDASC(this:imageFile, that:imageFile) -> Bool {
  return this.fileID > that.fileID}

然后用作:

images.sort(by: sorterForFileIDASC)


查看完整回答
反對(duì) 回復(fù) 2019-06-19
?
叮當(dāng)貓咪

TA貢獻(xiàn)1776條經(jīng)驗(yàn) 獲得超12個(gè)贊

幾乎每個(gè)人都給多么,怎樣讓我直接展示一下它的演變過程:

可以使用Array的實(shí)例方法:

// general form of closureimages.sortInPlace({ (image1: imageFile, image2: imageFile) -> Bool in return image1.fileID > image2.fileID })
// types of closure's parameters and return value can be inferred by Swift, so they are omitted along with the return arrow (->)
images.sortInPlace({ image1, image2 in return image1.fileID > image2.fileID })// Single-expression closures can implicitly return the result 
of their single expression by omitting the "return" keywordimages.sortInPlace({ image1, image2 in image1.fileID > image2.fileID })
// closure's argument list along with "in" keyword can be omitted, $0, $1, $2, and so on are used to refer the closure's first, second,

 third arguments and so onimages.sortInPlace({ $0.fileID > $1.fileID })// the simplification of the closure is the sameimages = images.sort
 ({ (image1: imageFile, image2: imageFile) -> Bool in return image1.fileID > image2.fileID })images = images.sort({ image1, image2 in return 
 image1.fileID > image2.fileID })images = images.sort({ image1, image2 in image1.fileID > image2.fileID })images = images.sort({ $0.fileID > 
 $1.fileID })

有關(guān)排序工作原理的詳細(xì)說明,請(qǐng)參閱排序函數(shù).


查看完整回答
反對(duì) 回復(fù) 2019-06-19
  • 3 回答
  • 0 關(guān)注
  • 750 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)