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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:20:31+00:00 2026-06-14T08:20:31+00:00

Below is my code: typedef struct { unsigned page; unsigned slot; } RID; //Below

  • 0

Below is my code:

typedef struct
{
  unsigned page;
  unsigned slot; 
} RID;

//Below struct has the Key on which I want to apply the sorting
struct LeafDataEntry
{
    void *key;
    RID rid;
};

//This is the sorting function I am using
bool leadNode_Key_asc( const LeafDataEntry &a, const LeafDataEntry &b){

    return strcoll((char *)a.key, (char *)b.key) > 0;
            //(strcmp((char *)a.key, (char *)b.key) > 0);
}

int main(){

    vector<LeafDataEntry> lde;

    char a[4] = {'D', 'B', 'C', 'D'};
    RID aRID = {0,0};
    char b[4] = {'A', 'C', 'B', 'A'};
    RID bRID = {0,1};

    unsigned size = sizeof(unsigned);

    lde.resize(2);
    char *tempPtr = (char *)malloc(8 + sizeof(RID));

    memcpy(tempPtr, &size, 4);
    tempPtr += 4;
memcpy(tempPtr, a, 4);

    tempPtr -= 4;
    lde[0].key = malloc(8);
    memcpy(lde[0].key, tempPtr, 8);
    memcpy(&lde[0].rid, &aRID, sizeof(RID));

    memcpy(tempPtr, &size, 4);
    tempPtr += 4;
    memcpy(tempPtr, b, 4);

    tempPtr -= 4;
    lde[1].key = malloc(8);
    memcpy(lde[1].key, tempPtr, 8);
    memcpy(&lde[1].rid, &bRID, sizeof(RID));

    std::sort(lde.begin(), lde.end(), leadNode_Key_asc);

    cout << "Sorted Data :: " << endl;
    for(int j=0; j<2; j++){
        cout << "KEY :: " << (char *)(lde[j].key);
        cout << ", RID ::" << "{" << lde[j].rid.pageNum << ", " <<        
    lde[j].rid.slotNum << "}";
  }
return 0;
}

I want to sort the above lde vector based on the *key value. It’s not working with the way given above.

NOTE: I cannot change the data types of any of the structs listed above.

  • 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-14T08:20:33+00:00Added an answer on June 14, 2026 at 8:20 am

    Well there are two problems in your code:

    first, you’re saving string in key of LeafDataEntry as [4 bytes for size of the string] + [the string] but strcoll takes strings ended with ‘\0’ and without size leading them, as you said that you don’t want any change in your datatypes, this would solve your problem:

    bool leadNode_Key_asc( const LeafDataEntry &a, const LeafDataEntry &b){
        size_t size1 = *(unsigned*)(a.key), size2 = *(unsigned*)(b.key);
        string a1 = string((char*)a.key + 4, size1), a2 = string((char*)b.key + 4, size2);
        return a1 < a2;
    }
    

    second, these two lines:

        cout << ", RID ::" << "{" << lde[j].rid.pageNum << ", " <<        
        lde[j].rid.slotNum << "}";
    

    should be replaced with:

        cout << ", RID ::" << "{" << lde[j].rid.page << ", " <<        
        lde[j].rid.slot << "}";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the code below: typedef struct {int a;} test_t; arbitrary_t test_dosomething(test_t* test) { if
Consider a code excerpt below: typedef struct tagTHREADNAME_INFO { DWORD dwType; LPCTSTR szName; DWORD
I have a code which uses bit-fields declared as follows typedef struct my{ const
I have the below code in stdafx.h. using namespace std; typedef struct { DWORD
I write the code below to test the talloc: #include talloc.h typedef struct linklist
I have a structure as shown below. typedef struct { attribute_code_t field_id; uint8_t instance_num;
Below is the implementation of my queue, which has functionality of enqueueing and dequeing
The code below compiles, but has different behavior for the char type than for
This code below compiles fine with VS2010 but doesn't want to compile with gcc
Edit: The code below has been modified to work as the problem has been

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.