首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
表格排序
[编程题]表格排序
热度指数:5914
时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 256M,其他语言512M
算法知识视频讲解
系统会在tbody中随机生成一份产品信息表单,如html所示。
请完成 sort 函数,根据参数的要求对表单所有行进行重新排序。
1、type为id、price或者sales,分别对应第1 ~ 3列
2、order为asc或者desc,asc表示升序,desc为降序
3、例如 sort('price', 'asc') 表示按照price列从低到高排序
4、所有表格内容均为数字,每一列数字均不会重复
5、不要使用第三方插件
马上挑战
算法知识视频讲解
提交运行
算法知识视频讲解
添加笔记
求解答(5)
邀请回答
收藏(74)
分享
提交结果有问题?
39个回答
12篇题解
开通博客
轻飘飘过wind
发表于 2022-09-03 01:17:41
1. 易读 2. 排序灵活 function sort(type, order) { const tbody = document.getElementById("jsList");  
展开全文
牛客979740006号
发表于 2021-06-11 11:04:43
function sort(type, order) { //type为id、price或者sales; asc表示升序,desc为降序 // 1.获取父节点 var tbody = document.getElementById('jsList') // 2.获
展开全文
前端小菜鸡QAQ
发表于 2021-07-08 15:35:01
function sort(type, order) { const tbody = document.querySelector('#jsList') let idx = 0 if (type === 'price') { idx = 1 } els
展开全文
mooringniu
发表于 2022-05-24 21:10:19
function sort(type, order) { let list = []; let table = d
展开全文
雾里看花花里看雾
发表于 2023-07-07 13:16:30
思路:首先将html中的tbody表格以对象数组的形式存储起来,即外部结构是数组,内部元素是对象,然后再对数组进行排序即可。首先获取tbody,然后获取tbody中的所有tr,接着获取tr中的所有td,再对td进行处理即可得到对象,再将各个对象加入数组中即可。 <body> &l
展开全文
Vemon.
发表于 2023-04-26 14:31:05
function sort(type, order) { // 获取需要排序的列的索引 const theadIndex = Array.from(document.querySelectorAll('th')).findIndex(item=> item.innerT
展开全文
17c89
发表于 2024-03-01 11:10:18
<script type="text/javascript"> // 填写JavaScript function sort(type, order) { const table = document.getElementById(&qu
展开全文
牛客606162351号
发表于 2023-05-27 23:41:37
function sort(type, order) { const frag = document.createDocumentFragment(); const trList = [].slice.call(jsList.children,0); const len =
展开全文
禾几元
发表于 2022-02-01 11:36:35
function sort(type, order) { const tdIndex = ["id", "price", "sales"].indexOf(type); // 获取要排序列的索引 const container = document.querySelector
展开全文
littlecat7
发表于 2023-01-11 15:36:44
function sort(type, order) { let index = { id: 0, price: 1, sales: 2 }[type]; let rows = Array.from(jsList.rows) if (order == 'asc') { row
展开全文
问题信息
C++工程师
iOS工程师
安卓工程师
运维工程师
模拟
前端工程师
算法工程师
2018
PHP工程师
百度
排序
Java工程师
上传者:
小小
难度:
39条回答
74收藏
4683浏览
热门推荐
通过挑战的用户
查看代码
笨蛋小温
2023-06-29 13:57:21
牛客26729...
2023-06-27 14:41:09
yuchenii
2023-06-25 21:22:35
zzzzz9
2023-06-25 16:06:23
牛客68455...
2023-06-20 14:24:13
相关试题
一个文件里有10万个随机正整数,按...
去哪儿
堆
模拟
评论
(4)
仅用O(1)的空间,将整数数组按奇...
百度
2011
C++
Java
编程基础
Java工程师
C++工程师
评论
(25)
来自
百度2011研发工程师笔试卷
下面描述中,符合结构化程序设计风格...
搜狐
Java工程师
C++工程师
iOS工程师
安卓工程师
运维工程师
前端工程师
算法工程师
PHP工程师
2018
评论
(1)
设某二叉树的先序遍历序列为abdg...
树
评论
(1)
2022 诺瓦科技 Perl re...
perl
System Verilog
评论
(1)
扫描二维码,关注牛客网
意见反馈
下载牛客APP,随时随地刷题
body,html{ padding: 0; margin: 0; font-size: 14px; color: #000000; } table{ border-collapse: collapse; width: 100%; table-layout: fixed; } thead{ background: #3d444c; color: #ffffff; } td,th{ border: 1px solid #e1e1e1; padding: 0; height: 30px; line-height: 30px; text-align: center; }
<table> <thead> <tr><th>id</th><th>price</th><th>sales</th></tr> </thead> <tbody id="jsList"> <tr><td>1</td><td>10.0</td><td>800</td></tr> <tr><td>2</td><td>30.0</td><td>600</td></tr> <tr><td>3</td><td>20.5</td><td>700</td></tr> <tr><td>4</td><td>40.5</td><td>500</td></tr> <tr><td>5</td><td>60.5</td><td>300</td></tr> <tr><td>6</td><td>50.0</td><td>400</td></tr> <tr><td>7</td><td>70.0</td><td>200</td></tr> <tr><td>8</td><td>80.5</td><td>100</td></tr> </tbody> </table>
function sort(type, order) { }