1

I have an array of strings (they were urls that are casted to strings), in the middle of each string they all have a UUID().uuidString with a count variable and a .jpg in them.

let arr = [ htps://firebasestorage.googleapis.com/myApp.appspot.com/o/users%Mxd6EUO5l2LK-mKa%2F4606E275-B2C5-4A69-B997-01423ABFE3B7%2FBE26726D-B8E5-47C8-9A18-504D23B99090_3.jpg?alt=media&token=e215e6a1-f5b9-431e-83a3,

htps://firebasestorage.googleapis.com/myAapp.appspot.com/o/users%-Ll_Mxd6EUO5l2LK-mKa%2F4606E275-B2C5-4A69-B997-01423ABFE3B7%2FBE26726D-B8E5-47C8-9A18-504D23B99090_1.jpg?alt=media&token=f350cf36-4c4e-4faf,

htps://firebasestorage.googleapis.com/myAPp.appspot.com/o/users%mKa%2F4606E275-B2C5-4A69-B997-01423ABFE3B7%2FBE26726D-B8E5-47C8-9A18-504D23B99090_2.jpg?alt=media&token=123uyqtr

....]

The first element has this in the middle of it: 2FBE26726D-B8E5-47C8-9A18-504D23B99090_3.jpg

The second element has this in the middle of it: 2FBE26726D-B8E5-47C8-9A18-504D23B99090_1.jpg

The third element has this in the middle of it: 2FBE26726D-B8E5-47C8-9A18-504D23B99090_2.jpg

The fourth element and on and on ..

How can I sort these strings in this array based on either the substring of the UUID with the _x.jpg or just the _x.jpg alone?

FYI I have access to the UUID beforehand

11
  • You want to sort them by the number of the jpg? Commented Aug 6, 2019 at 6:46
  • yes because that's what they all have in common that can be used for sorting Commented Aug 6, 2019 at 6:46
  • Split your string with _ . Here first part is UDID and second part is the x.jpg . Now you can sort accordingly. Commented Aug 6, 2019 at 6:55
  • @dahiya_boy thanks for the advice. I'm going to Shahid's answer. It's basically what you said Commented Aug 6, 2019 at 7:04
  • Rather than mapping your UUID into URLs and then Strings, make them into a [UUID: URL]. Sort the dict by its keys, and then take its values. For these purposes [(UUID, URL)] would work just as well, too. Commented Aug 6, 2019 at 7:11

2 Answers 2

2

You can sort the array this way

  • Convert the strings (back) to URL.
  • Get the lastPathComponent of each URL.
  • extract the substring from the last underscore character to the end.
  • Compare the strings with compare: and numeric option or localizedStandardCompare:
Sign up to request clarification or add additional context in comments.

Comments

1

If your starting array is something like this

let array = ["htps://firebasestorage.googleapis.com/myApp.appspot.com/o/users%Mxd6EUO5l2LK-mKa%2F4606E275-B2C5-4A69-B997-01423ABFE3B7%2FBE26726D-B8E5-47C8-9A18-504D23B99090_3.jpg?alt=media&token=e215e6a1-f5b9-431e-83a3","htps://firebasestorage.googleapis.com/myAapp.appspot.com/o/users%-Ll_Mxd6EUO5l2LK-mKa%2F4606E275-B2C5-4A69-B997-01423ABFE3B7%2FBE26726D-B8E5-47C8-9A18-504D23B99090_1.jpg?alt=media&token=f350cf36-4c4e-4faf","htps://firebasestorage.googleapis.com/myAPp.appspot.com/o/users%mKa%2F4606E275-B2C5-4A69-B997-01423ABFE3B7%2FBE26726D-B8E5-47C8-9A18-504D23B99090_2.jpg?alt=media&token=123uyqtr"]

Give this one a try.

You can split based on your jpg suffix, and then based on your UUID.

let sortedArray = array.sorted { (first, second) -> Bool in

let firstIndex = Int((first.components(separatedBy: ".jpg")[0]).components(separatedBy: "FBE26726D-B8E5-47C8-9A18-504D23B99090_")[1]) ?? -1
let secondIndex = Int((second.components(separatedBy: ".jpg")[0]).components(separatedBy: "FBE26726D-B8E5-47C8-9A18-504D23B99090_")[1]) ?? -1
return firstIndex < secondIndex
}

7 Comments

Different devices have different UDID so static separation with UDID like this is not good.
@SShahid I am no where seeing that OP is said its common. Can you highlight. If its common then it must be common for a user not for the entire users.
@SShahid I just tried it in playground, it didn't work. It printed out exactly the same
I have added the starting array that I used. Are you using something else?
Nope, same exact starting array. Let c+p your and see what happens
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.