0

The question is how to sort an Array of Arrays with the following structure:

status = 
 [["Wartend", :pending],
  ["Schufa Check", :schufa_check],
  ["Schufa Key existiert", :schufa_person_key_exists],
  ["LottoIdent", :lotto_ident],
  ["IBAN existiert", :iban_exists],
  ["E-Mail Bestätigung", :email_validation],
  ["SMS Bestätigung", :mobile_validation],
  ["Aktiv", :active],
  ["gesperrt", :locked],
  ["ausgeschlossen", :locked_out],
  ["werden gelöscht", :marked_for_deletion]]

The result should be this:

[["Aktiv", :active], 
 ["ausgeschlossen", :locked_out], 
 ["E-Mail Bestätigung", :email_validation], 
 ["gesperrt", :locked], 
 ["IBAN existiert", :iban_exists], 
 ["LottoIdent", :lotto_ident], 
 ["Schufa Check", :schufa_check], 
 ["Schufa Key existiert", :schufa_person_key_exists], 
 ["SMS Bestätigung", :mobile_validation], 
 ["Wartend", :pending], 
 ["werden gelöscht", :marked_for_deletion]]

2 Answers 2

5
p status.sort_by{|ar| ar.first.downcase}
# =>[["Aktiv", :active], ["ausgeschlossen", :locked_out], ["E-Mail Bestätigung", :email_validation], ["gesperrt", :locked], ["IBAN existiert", :iban_exists], ["LottoIdent", :lotto_ident], ["Schufa Check", :schufa_check], ["Schufa Key existiert", :schufa_person_key_exists], ["SMS Bestätigung", :mobile_validation], ["Wartend", :pending], ["werden gelöscht", :marked_for_deletion]]
Sign up to request clarification or add additional context in comments.

Comments

2

A common error is to forget, that the String (in each Array at position a[0]) is upper or lowercase. Naturally the sorting will result in something like:

 "Aktiv",  
 "E-Mail Bestätigung",  
 "IBAN existiert",  
 "LottoIdent",  
 ...,  
 "SMS Bestätigung",  
 "ausgeschlossen",    
 "gesperrt"  

So the solution is to use the common Ruby sort method with a block and to downcase the values at position a[0]:

status.sort{|x,y| x[0].downcase <=> y[0].downcase}

2 Comments

or status.sort_by{|x| x[0].downcase}.
is there an additional flag i can use to determine if it's ascending or descending

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.