You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Notepad++中按组对文本按第三列局部排序

在Notepad++中实现分组按指定列排序的方法

方法一:Python Script插件(推荐,适合大量数据)

Notepad++基础排序功能无法直接实现分组内排序,借助Python Script插件可精准处理:

  1. 安装插件:打开Notepad++,点击「插件」→「插件管理」,搜索「Python Script」并完成安装。
  2. 创建排序脚本:
    • 点击「插件」→「Python Script」→「New Script」,命名为sort_group_by_col3.py。
    • 粘贴以下代码(假设列以空格分隔,若为制表符则将split()改为split('\t')):
      editor.beginUndoAction()
      # 读取所有非空行
      lines = [line.rstrip('\n') for line in editor.getText().split('\n') if line.strip()]
      group_dict = {}
      
      # 按前两列分组
      for line in lines:
          cols = line.split()
          if len(cols) >= 3:
              group_key = (cols[0], cols[1])
              if group_key not in group_dict:
                  group_dict[group_key] = []
              # 存储第三列数值和整行内容,方便排序
              group_dict[group_key].append( (int(cols[2]), line) )
      
      # 每组内按第三列升序排序,再拼接结果
      sorted_result = []
      # 若需要组间按前两列排序,保留sorted(group_dict.keys()),否则直接遍历group_dict.values()
      for key in sorted(group_dict.keys()):
          sorted_lines = [line for num, line in sorted(group_dict[key], key=lambda x: x[0])]
          sorted_result.extend(sorted_lines)
      
      # 替换原文本内容
      editor.setText('\n'.join(sorted_result))
      editor.endUndoAction()
      
  3. 执行脚本:打开目标文本文件,点击「插件」→「Python Script」→「Scripts」→选择刚才创建的脚本,即可完成分组排序。

方法二:手动辅助排序(适合小量数据)

如果数据分组较少,可手动操作:

  • 先按前两列分组:点击「编辑」→「行操作」→「按列排序」,选择前两列作为排序依据,将相同前两列的行聚集到一起。
  • 再每组内排序:选中同一组的所有行,再次点击「编辑」→「行操作」→「按列排序」,选择第三列作为排序依据,升序排列即可。

内容的提问来源于stack exchange,提问作者MathArt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 21:30:09