title: Ruby nested iteration to match strings within an array
body: <p>I'm trying to figure out from an array of strings how to compare strings based on characters. So for example there's the following:</p>
  
  <pre><code>arr = [4, "string", "gnirts", "strign", "ta", "atc"]
  </code></pre>
  
  <p>In this case "ta" and "atc" do not match the other strings. But "string", "gnirts", "strign" match.</p>
  
  <p>My first thought would be to break the array apart, do a length check. Then compare the strings and keep the first present. </p>
  
  <p>I know I don't care about the 4. It's just indicating the number of strings so I can do an arr.shift(1).</p>
  
  <p>I know I could do something like string.chars.sort but how do I compare the strings within the arrays?</p>
  
  <p>I was thinking something like:</p>
  
  <pre><code>arr.each_with_index do |value, index|
   index.each do |item|
    if value.chars.sort == item
     return value
   end
  end
  </code></pre>
  
  <p>That definitely does not work.</p>
  
  <p>What I'm wanting to end up seeing would then have a sort on the array so I'd end up with atc, gnirts, and string (because the others match against it and string comes first).</p>
  
  <p>So how do I ultimately compare strings within an array and keep the first one numerically?</p>
  
  <p>EDIT:
  Input would be something like [4, "string", "gnirts", "strign", "ta", "atc"]
  Output array would be </p>
  
  <pre><code>["atc", "ta", "string"]
  </code></pre>
  
  <p>So matching would keep the first present and then sort on those not matched.</p>