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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:21:01+00:00 2026-06-18T06:21:01+00:00

I have a vector that I want to sort alphabetically. I have successfully been

  • 0

I have a vector that I want to sort alphabetically. I have successfully been able to sort it by one indexes value alphabetically, but when I do it only changes the order of that index and not the entire vector. How can I get it to apply the order change to the entire vector?
This is my current code I am running:

std::sort (myvector[2].begin(), myvector[2].end(), compare);

bool icompare_char(char c1, char c2)
{
  return std::toupper(c1) < std::toupper(c2);
}

bool compare(std::string const& s1, std::string const& s2)
{
  if (s1.length() > s2.length())
    return true;
  if (s1.length() < s2.length())
    return false;
  return std::lexicographical_compare(s1.begin(), s1.end(),
                                      s2.begin(), s2.end(),
                                      icompare_char);
}

My general structure for this vector is vector[row][column] where:

| One | Two | Three |
|  1  |  2  |   3   |
|  b  |  a  |   c   |

For example if I had a vector:

myvector[0][0] = 'One' AND myvector[2][0]='b'
myvector[0][1] = 'Two' AND myvector[2][1]='a'
myvector[0][2] = 'Three' AND myvector[2][2]='c'

| One | Two | Three |
|  1  |  2  |   3   |
|  b  |  a  |   c   |

And I sort it I get:

myvector[0][0] = 'One' AND myvector[2][0]='a'
myvector[0][1] = 'Two' AND myvector[2][1]='b'
myvector[0][2] = 'Three' AND myvector[2][2]='c'

| One | Two | Three |
|  1  |  2  |   3   |
|  a  |  b  |   c   |

and not what I want:

myvector[0][0] = 'Two' AND myvector[2][0]='a'
myvector[0][1] = 'One' AND myvector[2][1]='b'
myvector[0][2] = 'Three' AND myvector[2][2]='c'

| Two | One | Three |
|  2  |  1  |   3   |
|  a  |  b  |   c   |

I looked around for a good approach but could not find anything that worked… I was thinking something like:

std::sort (myvector.begin(), myvector.end(), compare);

Then handle the sorting of the third index within my compare function so the whole vector would get edited… but when I passed my data I either only changed the order in the function and still did not change the top layer or got errors. Any advice or help would be greatly appreciated. Thank you in advance.

  • 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-18T06:21:02+00:00Added an answer on June 18, 2026 at 6:21 am

    Ideally, merge the 3 data fields into a struct so that you can have just 1 vector and so sort it simply.

    struct DataElement{
        std::string str;
        char theChar;
        int num;
        bool operator<(const DataElement& other)const{return theChar<other.theChar;}
    };
    
    std::vector<DataElement> myvector;
    
    std::sort (myvector.begin(), myvector.end());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose that I have a vector of key-value pairs that I want to put
I have a vector that I want to use to create a heap. I'm
I have a vector X that contains positive numbers that I want to bin/discretize.
I have a vector of beans that holds information I want to display in
I have a Vector receiptOrder and I want to print that receipt to thermal
I have a std::vector. I want to create iterators representing a slice of that
I have a vector that that stores Node*. A Node has the member properties:row,col,value.
I have a vector of Student objects which I want to sort using #include
I have a one dimensional vector of data in R and I want to
I have a linked list that I want to sort part of, eg: std::sort(someIterator,

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.