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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:43:52+00:00 2026-05-13T00:43:52+00:00

In my database I store a number of topics and examples. Each example belongs

  • 0

In my database I store a number of “topics” and “examples”. Each example belongs to one topic.

I’d like show all the topics by name ASC in a tree component except for one topic (Topic C) which I always want on top. This may though come to change later and I’d rather not change the basic JSON output of topics and examples from the DB.

1) Does it seem reasonable to generate the “unordered list” structure client side (or should I, already in the json_encode() phase make sure the order is appropriate?) and

2) how would I, in that case, filter one specific topic node by name and put it first in the sequence?

  • Topic C
    • Example
    • Example
  • Topic A
    • Example
    • Example
  • Topic B
    • Example
    • Example
  • 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-13T00:43:52+00:00Added an answer on May 13, 2026 at 12:43 am

    Your questions are very difficult to answer without knowing exaclty the reasons you’re doing this!

    Anyway, in attempting to answer,
    1) :

    • If you are generating a list in HTML on the server anyway, and the order of the list doesn’t change, why do it with JSON & JavaScript? Use your server side tech to do this.

    • If the list will change and you want something in the UI on the client to manage the order of the list and you have accessibility considerations: create your list and your JSON using your server side tech, order both server side and then render your HTML list and pass your JSON to the client. This will make the list available to client (user) without JavaScript enabled and the ‘sortable list’ available to JavaScript enabled clients.

    • If you don’t have any concern for users without JavaScript AND the list needs to be reorderable then just pass your JSON data to your client and use JavaScript to render it and handle the reordering.

    2) I’m assuming that each of your objects that you want to sort is in an array? If they are then I would use the Array.sort method : http://www.w3schools.com/jsref/jsref_sort.asp

    Something like the following should give you an idea of how to go about doing this:

           var data = [{
        "Topic" : "C",
        "Examples":[
         {"Example" : "Example A one"},
         {"Example" : "Example A two"},
        ] 
       },{
        "Topic" : "B",
        "Examples":[
         {"Example" : "Example B one"},
         {"Example" : "Example  B two"},
        ] 
       },{
        "Topic" : "A",
        "Examples":[
         {"Example" : "Example C one"},
         {"Example" : "Example C two"},
        ] 
       }];
    
       function sortTopics(topics, firstItem){
        var sortedArray = []; 
    
        for (var i = 0; i < topics.length; i++) {
         if (topics[i].Topic == firstItem) {
          //add the specified first item
          sortedArray.push(topics[i]);
          //remove element from original
          topics.splice(i,1);
    break;
         }
    
        }
    
        //sort the remainder of the array
        topics.sort(function(a,b){
         if(a.Topic < b.Topic){return -1;}
         if(a.Topic > b.Topic){return 1;}
         return 0;
        });
    
        //loop though and add each item to the new array          
        for (var i = 0; i < topics.length; i++) {
         sortedArray.push(topics[i]);
        }
    
        return sortedArray;
       }
    
       sortTopics(data, "C");
    

    I’m not sure how cross browser it would be and obviously you’ll need to generate the list from the JSON but it should give you somewhere to start, you could make it all more generic and efficient of course!

    Good luck.

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

Sidebar

Related Questions

No related questions found

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.