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

  • SEARCH
  • Home
  • 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 9243877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:54:17+00:00 2026-06-18T08:54:17+00:00

I have a task in which I have a labeled numeric vector, say size

  • 0

I have a task in which I have a labeled numeric vector, say size 5 and labels a, b, c, d, e, and I need to sort it and then print the labels in (inverse) sorted order. So for example, given this vector v1 in input:

     a   b    c   d     e
1   -3  -1   10   5   -15

the required output would be: cdbae.

Now, the hard part is handling ties. I need to print all possible orders in case of a tie. So for example, given this other vector v2 in input:

     a    b    c    d    e
1   10   29   10   10  -15

After sorting we’d have:

     b    a   c    d    e
1   29   10  10   10  -15

But of course we have 3! = 6 possible permutations. I would like it to print this array:

v <- c("bacde", "badce", "bcade", "bcdae", "bdcae", "bdace").

If that helps, the number of labels is never more than 10 so I don’t mind performance relative to that, for that matter.

  • 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-06-18T08:54:18+00:00Added an answer on June 18, 2026 at 8:54 am

    This does it. But I wouldn’t recommend running on dataset with too many ties.

    require(gregmisc)
    x <- c(a=10, b=29, c=10, d=10, e=-15)
    y <- sort(x, decreasing=T)
    
    if (any(duplicated(y))) {
        o <- sapply(unique(y), function(val) {
            m <- names(y[y==val])
            # just to make things quicker using length(m)
            if (length(m) <= 1) {
                return(m)
            }
            do.call(paste0, as.data.frame(permutations(length(m), length(m), m)))
        })
        out <- do.call(paste0, expand.grid(o))
    } else {
        out <- paste(names(y), collapse="")
    }
    
    # [1] "bacde" "badce" "bcade" "bcdae" "bdace" "bdcae"
    

    Of course it handles multiple ties as well. Running on:

    x <- c(a=10, b=29, c=10, d=-10, e=35, f=-10, g=10)
    y <- sort(x, decreasing = TRUE)
    

    gives:

    # [1] "ebacgdf" "ebagcdf" "ebcagdf" "ebcgadf" "ebgacdf" "ebgcadf" "ebacgfd" "ebagcfd"
    # [9] "ebcagfd" "ebcgafd" "ebgacfd" "ebgcafd"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a task which I need to run in the background in my
I have an automating task in which I need to fill several forms in
I have given a task in which I need to encrypt an user's ID
I have a task which i need to cancel if the wait time is
i have task which takes a parameter and has three modes of results Example
I have a task which has to be run everyday. I will get just
I have a model Task which will HABTM many TaskTargets. When it comes to
I'm using dhtmlx Gantt Chart UI component which have task list and graphical chart.
I have an interesting scenario. I have a task model which has a task
This is a simplified task which I have to solve in real project. In

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.