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 8684017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:11:30+00:00 2026-06-12T22:11:30+00:00

It probably easier explain myself using an example. Let’s say I have a list

  • 0

It probably easier explain myself using an example.
Let’s say I have a list s:

s <- list( c(5,3,4,3,6),c(“A”,”B”,”C”,”D”,”E”))

s has always the same number of object for all sub-vectors. NA value are not allowed. The vectors contain different types.

What I want to achieve is:

rank  v1   v2
1     3    "B"
2     3    "D"
3     4    "C"
4     5    "A"
5     6    "E"

Basically, to sort the list based on the first vector (in ascending order) and then (in case on tie) look to the second vector using the lexicological order. In C++ world the only thing that I need to do is to define the operator< for my object, however I am pretty new of R and I am running out ideas.

The best strategy that I have found is to loop over the elements and calculate a rank value (double) for each couple (eg. 3 “B” will result with the highest rank and 6 “E” with the lowest), store the results in another vector and sort it. However the solution is not great because find a good ranking function can be tricky and it is not very easy to generalize.

It seems to me such a common problem that it has to be a better way. Can anyone point me in the right direction?

Thanks for your help.

  • 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-12T22:11:32+00:00Added an answer on June 12, 2026 at 10:11 pm

    Use order():

    s <- data.frame(v1=c(5,3,4,3,6), v2=c("A","B","C","D","E"))
    s[order(s$v1, s$v2), ]
      v1 v2
    2  3  B
    4  3  D
    3  4  C
    1  5  A
    5  6  E
    

    Note that I transformed your list to a data frame. Since a data frame is itself a list (with all elements the same length) this shouldn’t be a problem in your case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Probably an example will be easier to explain this. So imagine i have something
I have below probably a bunch of problems but let's simplify them with easier
This is probably easier than I am making it, but basically what I need
Probably something really simple, but I'm having some real trouble with it. I have
Probably a very trivial problem. I have an object that looks like this: @PersistenceCapable
Probably I'm just too dump for googling, but I always thought char arrays get
Probably something simple, but heres my problem, I have implememnted a simple captcha into
Probably not. But still would be nice to have it. Kinda hard to believe
I probably won't be able to explain this well enough, but here it goes...
I have a list of items I want to feed to an API via

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.