0

Tried to sort array objects per specific array column, in my case, i tried to sort it by 'title' but it does not work, I want to sort it alphabetically, refer below

$(function(){
 
  var arr = [];
  
  $('table tbody tr').each(function(){
   
    arr.push({ title : $.trim($(this).find('td:first-child').text()), column : $(this).index() });
    
  });
 
  
arr.sort(function(a,b){
   return a.title - b.title
 });
 
 console.log(arr);
 
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<table class="table-bordered table table-responsive table-striped table-b34d4e">
    <thead>
        <tr>
            <th>Course</th>
            <th>Level</th>
            <th>Trainer</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
                <a target="_blank" href="appointment-of-agents-and-salesperson.html">
                 Law Pertaining to Appointment of Real Estate Agent & Salesperson (C1L1S0014)
                </a>
            </td>
            <td>
                1
            </td>
            <td>
                Celeste Ng
            </td>
        </tr>
        <tr>
            <td>
                <a target="_blank" href="conflict-of-interest-and-how-to-avoid-them.html">
                 Conflict of Interest and How to Avoid Them (C1L1S0004)
                </a>
            </td>
            <td>
                1
            </td>
            <td>
                Celeste Ng
            </td>
        </tr>
        <tr>
            <td>
                <a target="_blank" href="understanding-the-estate-agents-act.html">Understanding the Estate Agent Act (C1L1S0023)</a>
            </td>
            <td>
                1
            </td>
            <td>
                Celeste Ng
            </td>
        </tr>
        <tr>
            <td>
                <a target="_blank" href="art-of-effective-negotiation-and-closing.html">Art of Effective Negotiations and Closing (C1L1S0598)</a>
            </td>
            <td>
                1
            </td>
            <td>
                Nicholas Nan
            </td>
        </tr>
        <tr>
            <td>
                <a target="_blank" href="attaining-service-excellence-in-real-estate-industry.html">Attaining Service Excellence in Real Estate Industry (C1L1S0563)</a>
            </td>
            <td>
                1
            </td>
            <td>
                Chris Koh
            </td>
        </tr>
        <tr>
            <td>
                <a target="_blank" href="cea's-disciplinary-cases-2015.html">CEA's Disciplinary Cases 2015 (C1L1S0553)</a>
            </td>
            <td>
                1
            </td>
            <td>
                Celeste Ng
            </td>
        </tr>
        <tr>
            <td>
                <a target="_blank" href="law-pertaining-to-appointment-of-real-estate-agent-and-salesperson.html">Law Pertaining to Appointment of Real Estate Agent & Salesperson (C1L1S0014) </a>
            </td>
            <td>
                1
            </td>
            <td>
                Celeste Ng
            </td>
        </tr>
        <tr>
            <td>
                <a target="_blank" href="duties-of-tenants-agent-with-case-studies-on-private-residential-properties.html">Duties of Tenant’s Agent with Case Studies on Private Residential Properties (C1L20582)</a>
            </td>
            <td>
                2
            </td>
            <td>
                Celeste Ng
            </td>
        </tr>
        <tr>
            <td>
                <a target="_blank" href="duties-of-landlords-agent-with-case-studies-on-private-residential-and-commercial-properties.html">Duties of Landlord’s Agent with Case Studies on Private Residential and Commercial Properties (C1L2S0365)</a>
            </td>
            <td>
                2
            </td>
            <td>
                Celeste Ng
            </td>
        </tr>
    </tbody>
</table>

any help, ideas please?

Juliver Galleto
  • 8,831
  • 27
  • 86
  • 164

0 Answers0