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

The Archive Base Latest Questions

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

I want to sort list items by their priority, which the user types in,

  • 0

I want to sort list items by their priority, which the user types in, and it does that well. However, when there is more than one item with the same priority, it doesn’t sort them by order of arrival like it’s supposed to.

I’m sorry if I’m not making this clear enough so you can understand. The names of the variables are in portuguese, so if you don’t understand someting, please ask.

Here is the code:

typedef struct pedido pedido, *ppedido;

struct pedido{
    char id[5];
    int prioridade;
    int mesa, n_pratos;
    struct prato *prato[TAM];
    ppedido prox;
};

struct prato{
    char id[5];
};

ppedido novo_pedido(ppedido lista)
{
    ppedido novo, aux, anterior = NULL;
    int i;

    novo = (struct pedido*)malloc(sizeof(pedido));

    if(novo == NULL){
        printf("Erro na alocacao de memoria...\n");
        return;
    }

    printf("Number of menus: ");
    scanf("%d", &novo->n_pratos);

    printf("Table number: ");
    scanf("%d", &novo->mesa);

    printf("Priority of request? ");
        scanf("%d", &novo->prioridade);

        printf("Introduza o ID do pedido: ");
        scanf("%s", &novo->id);

    for(i=0;i<novo->n_pratos;i++){
        printf("ID of menu %d: ", i+1);  //something like "M1, M4..." doesn't matter
        scanf("%s", &novo->prato[i]);
        fflush(stdin);
    }

    novo->prox=NULL;

    if(lista == NULL || novo->prioridade > lista->prioridade) { 
        novo->prox = lista; 
        lista = novo; 
    }
    else
    { 
        aux = lista;

        while(aux != NULL && novo->prioridade < aux->prioridade)   //this is where it should be sort requests by their priority and order of arrival
            aux = aux->prox; 
        novo->prox = aux->prox; 
        aux->prox = novo;
    }
    return lista;
}
  • 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-06T06:21:10+00:00Added an answer on June 6, 2026 at 6:21 am

    I think you want to change this:

    while(aux != NULL && novo->prioridade < aux->prioridade)
    

    To:

    while(aux->prox != NULL && novo->prioridade <= aux->prox->prioridade)
    

    This way it will go past all of the the ones of the same priority and be put closer to the end of the list. This will keep a reference to aux when you traverse to the end of the list.

    I assume in your search you stop as soon as you find the highest priority.

    This assumes the order of entry into the list is the same as the order of arrival.

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

Sidebar

Related Questions

I have List I want to sort Desc by Priority, which is int and
Is there a jQuery plugin that will allow to sort a list of items
I have a list of artists, albums and tracks that I want to sort
I use a ListView with android.R.layout.simple_list_item_multiple_choice as list items. I want to sort the
I have a list of items and their corresponding prices. I want to show
I want to sort a list by each item's digit. Example: myCmpItem = '511'
I want to sort a list of strings based on the string length. I
I have implemented a lambda function to sort a list. now i want to
I have a linked list, and I want to sort them by names (for
I have an array containing a list of files. I want to sort it

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.