0

how to check if an array is sorted?

I am sorting using sort descriptors. Is there any API to check if an array is already in sorted order in Swift/Objective-C.

Thanks

4
  • 2
    Sort the array and see if the result is the same. Or just sort it - now you know it's sorted. Commented Aug 3, 2015 at 20:50
  • 2
    Or even better — don't use sort descriptors. Sort the Swift way instead. Commented Aug 3, 2015 at 21:01
  • matt, just curious: what's the Swift way? Commented Aug 3, 2015 at 21:14
  • 1
    Using a closure with sort, I'd assume Commented Aug 3, 2015 at 21:21

2 Answers 2

1

i think there is no frame work, simply iterate truth the array, and check if the current element greater or equal (or less or equal, or which kind of sorting you look for) is. This is the easiest way. Look please at this Question Solution

Sign up to request clarification or add additional context in comments.

Comments

1

As far as I know, there isn't a built in way to check if an array is already sort descriptors. The best way to check is to iterate through the array and check if each element should come before the element precedes it (using whatever definition of "should come before" you want for your sort). If you're sorting custom objects, you can write some sort of compareTo method that compares two objects of your class, which will make it convenient to check using the method I described.

Comments

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.