Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6149685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:29:41+00:00 2026-05-23T19:29:41+00:00

I need to run both sort! and uniq! on an array. Which is better

  • 0

I need to run both sort! and uniq! on an array. Which is better to run first? Or is there a way to combine these into one command?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:29:41+00:00Added an answer on May 23, 2026 at 7:29 pm

    I made a little benchmark test with different combinations of uniq uniq! sort and sort!
    There are no significant differences:

                    user     system      total        real
    sort!.uniq!103.547000   0.172000 103.719000 (104.093750)
    uniq!.sort!100.437000   0.093000 100.530000 (100.859375)
    uniq.sort 100.516000   0.157000 100.673000 (101.031250)
    sort.uniq 103.563000   0.062000 103.625000 (103.843750)
    

    What you may not use is something like:

    array = [1]
    array.uniq!.sort!
    

    uniq! will result in nil and sort! will throw an exception.

    The benchmark I used:

    require 'benchmark'
    require 'date'
    
    TEST_LOOPS = 10_000
    ARRAY = []
    1000.times{ 
      ARRAY << Date.new(1900 + rand(100), rand(11)+1, rand(27) + 1 ) 
    }
    Benchmark.bm(10) {|b|
    
      b.report('sort!.uniq!') {
       TEST_LOOPS.times { 
          a = ARRAY.dup
          a.sort!
          a.uniq!
       }            #Testloops
      }             #b.report
    
      b.report('uniq!.sort!') {
       TEST_LOOPS.times { 
          a = ARRAY.dup
          # uniq!.sort! not possible. uniq! may get nil
          a.uniq!
          a.sort!
       }            #Testloops
      }             #b.report
    
      b.report('uniq.sort') {
       TEST_LOOPS.times { 
          a = ARRAY.dup.uniq.sort
       }            #Testloops
      }             #b.report
    
      b.report('sort.uniq') {
       TEST_LOOPS.times { 
          a = ARRAY.dup.sort.uniq
       }            #Testloops
      }             #b.report
    
    } #Benchmark
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have one vps server, at the moment, and we need to run both
Currently I need to run two queries to get both the total amount of
I have a number of reports that need to be both run separately and
I need to make a graphic interface in Lazarus from which I run miscelaneous
In a core assembly, which is run both in a windows service, and in
In my application i need to run my application of both versions android 2.1
I'm working with both libfuse and the glib event interface and I've run into
I need to run a query on 2 legacy tables but both the join
i need run code that will create a database and populate tables. i am
I need to run a JavaScript function onLoad(), but only do it if the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.