按特定数组对象列对数组进行排序

时间:2017-12-27 08:10:51

标签: javascript

尝试按特定数组列对数组对象进行排序,在我的情况下,我尝试按“标题”对其进行排序,但它不起作用,我想按字母顺序对其进行排序,请参阅下面的

$(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>

任何帮助,想法好吗?

0 个答案:

没有答案