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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:48:26+00:00 2026-05-22T20:48:26+00:00

Let me show us my scenario and my problem. This is a list of

  • 0

Let me show us my scenario and my problem.

This is a list of div that are ordered by the content of categories1 alphabetically by jQuery when the page is loaded :

<div id="container">
    <div class="categories">
        <div class="categories1">
            Ernest
        </div>
    </div>

    <div class="categories">
        <div class="categories1">
            Andy
        </div>
    </div>

    <div class="categories">
        <div class="categories1">
            Mark
        </div>
    </div>
</div>

// result
Andy
Ernest
Mark

Now, when I try to append an element and then ordering the elements, there is a trouble : in fact the last element inserted is not ordered, but it’s just appended at the end of the list :

<div class="categories">
    <div class="categories1">
        Buddy
    </div>
</div>

// result
Andy
Ernest
Mark
Buddy

This is the sorting function :

$(function() {
    var items = $('.categories1').get();

    items.sort(function(x,y) {
      return $(x).text() > $(y).text();
    });

    $('#container').empty();

    $(items).each(function() {
        $('#container').append($(this).parent());
    });
});

Why this behaviour? And how can I fix it?

  • 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-05-22T20:48:27+00:00Added an answer on May 22, 2026 at 8:48 pm

    Not sure if this is the best fix, but you could $.trim the text: http://jsfiddle.net/WEtML/4/

    items.sort(function(x,y) {
        return $.trim($(x).text()) > $.trim($(y).text());
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to let a div show and disappaer with jquery and css. The
For this question, let us assume that we will want to show the face
Let's me show first the scenario that is working: I have a HandlerAttribute that
Let me show what I need first so you can understand my problem. I
I am having a difficult time sorting through this PHP/MySQL issue. Let me show
a funny problem ... i can't understand it.. let me show you what i
Let's say that I have an application frame, and I want to show a
I can't seem to find anything that would let me to easily show a
Hi before going to direct problem let me show my code : //Definition of
To make my doubt clear, I have this scenario: I have an app that

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.