Jump to content

Rearrange in alphabetical order

Recommended Posts

Hi there,

I have a blog on books and I would like to organise them in alpahbetical order. All the book titles are entered as blog titles. Can anyone help me?  

Link to comment

This code worked for me to alphabetize blog posts based on title name:

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
 
  <script type="text/javascript">
      $(document).ready(function(){
          if( $('.blog-item').length != 0 ){
              var categories = []; 
              $('.blog-item').each(function () {
                categories.push({
                  categoryName: $(this).find('.blog-title').html(),
                  listItem: $(this)
                });
              });
              categories.sort(function(a,b) {
                var c = a.categoryName.toUpperCase();
                var d = b.categoryName.toUpperCase();
                return (c > d) ? 1 : ((d > c) ? -1 : 0);
              });
              $.each(categories, function(index, object){
                  object.listItem.appendTo(object.listItem.parent());
              });
          }
      });
  </script>

The only issue is that the pagination links at the bottom won't be alphabetized as well, so I had to hide them. If anyone has solved this problem I'd love to see that solution

Link to comment
On 1/14/2021 at 11:40 PM, Teucer90 said:

This code worked for me to alphabetize blog posts based on title name:


<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
 
  <script type="text/javascript">
      $(document).ready(function(){
          if( $('.blog-item').length != 0 ){
              var categories = []; 
              $('.blog-item').each(function () {
                categories.push({
                  categoryName: $(this).find('.blog-title').html(),
                  listItem: $(this)
                });
              });
              categories.sort(function(a,b) {
                var c = a.categoryName.toUpperCase();
                var d = b.categoryName.toUpperCase();
                return (c > d) ? 1 : ((d > c) ? -1 : 0);
              });
              $.each(categories, function(index, object){
                  object.listItem.appendTo(object.listItem.parent());
              });
          }
      });
  </script>

The only issue is that the pagination links at the bottom won't be alphabetized as well, so I had to hide them. If anyone has solved this problem I'd love to see that solution

If you share link to blog post, we can give code to hide it easier.

Email me if you have need any help (free, of course.). Answer within 24 hours. 
Or send to forum message

Contact Customer Care - Learn CSS - Buy me a coffee (thank you!)

Link to comment
On 1/11/2021 at 5:22 PM, Ekaland said:

r are you saying you want the books within the categories alphabetized: Category "G" - Books "A", "B",..."Z", Category "C" - Books "A", "B",..."Z"

This is what I would like please. Is this possible? 

Link to comment
  • 1 year later...
On 1/14/2021 at 11:40 AM, Teucer90 said:

This code worked for me to alphabetize blog posts based on title name:

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
 
  <script type="text/javascript">
      $(document).ready(function(){
          if( $('.blog-item').length != 0 ){
              var categories = []; 
              $('.blog-item').each(function () {
                categories.push({
                  categoryName: $(this).find('.blog-title').html(),
                  listItem: $(this)
                });
              });
              categories.sort(function(a,b) {
                var c = a.categoryName.toUpperCase();
                var d = b.categoryName.toUpperCase();
                return (c > d) ? 1 : ((d > c) ? -1 : 0);
              });
              $.each(categories, function(index, object){
                  object.listItem.appendTo(object.listItem.parent());
              });
          }
      });
  </script>

The only issue is that the pagination links at the bottom won't be alphabetized as well, so I had to hide them. If anyone has solved this problem I'd love to see that solution

@Teucer90 does this impact ALL blog items on a website? Like if I have 3 different types of blog pages on the website will they all be impacted by this?

Link to comment
48 minutes ago, hannah3 said:

does this impact ALL blog items on a website?

It depends on where the code is installed. If it is installed to Blog Settings > Advanced > Page Header Code Injection for the blog page, please see per-page code injection, then it will only effect that blog.

If it installed to Settings > Advanced > Code Injection > FOOTER, then it would be a site-wide effect.

A couple of notes. I don't know this code. Also simple code done client side is not sorting all your posts. It is only sorting the posts on the page. Unless you use a plug-in like Lazy Summaries as mentioned earlier, which works with summary blocks.

Find my contributions useful? Please like, upvote, mark my answer as the best ( solution ), and see my profile. Thanks for your support! I am a Squarespace ( and other technological things ) consultant open for new projects.

Link to comment

Hey, all! I was looking for this solution as well, but for the Archive block. I've edited the original code. Now the Archive block will display your posts in alphabetical order as well. Here's the code:

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
 
  <script type="text/javascript">
      $(document).ready(function(){
          if( $('.archive-item').length != 0 ){
              var categories = []; 
              $('.archive-item').each(function () {
                categories.push({
                  categoryName: $(this).find('.archive-item-link').html(),
                  listItem: $(this)
                });
              });
              categories.sort(function(a,b) {
                var c = a.categoryName.toUpperCase();
                var d = b.categoryName.toUpperCase();
                return (c > d) ? 1 : ((d > c) ? -1 : 0);
              });
              $.each(categories, function(index, object){
                  object.listItem.appendTo(object.listItem.parent());
              });
          }
      });
  </script>

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.