HarmonyOS 鸿蒙Next 搜索历史记录排列顺序问题?代码如下

HarmonyOS 鸿蒙Next 搜索历史记录排列顺序问题?代码如下 问题如下:主要问题在第三步

①搜索【啊】【啊啊】【啊啊啊】显示顺序正常

cke_93483.png

②再搜索【啊啊】顺序正常,但是index的值为什么是+2了?

cke_96196.png

③再搜索【啊啊啊啊】,排序不正常,没显示在顶部,且两次index的值相同?

cke_139277.png


更多关于HarmonyOS 鸿蒙Next 搜索历史记录排列顺序问题?代码如下的实战教程也可以访问 https://www.itying.com/category-93-b0.html

5 回复

你是想实现 最近最久未使用算法(LRU)吗?

①搜索【啊】【啊啊】【啊啊啊】显示顺序正常

cke_340.png

②再搜索【啊啊】顺序正常,但是index的值为什么是+2了?

cke_822.png

③再搜索【啊啊啊啊】,排序不正常,没显示在顶部,且两次index的值相同?

cke_1267.png

更多关于HarmonyOS 鸿蒙Next 搜索历史记录排列顺序问题?代码如下的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


感谢感谢,就是这个效果,原来是LRU算法,我研究一下,

打印了一下,只要你点击“搜索”你就会把静态的NextId+1

所以我换了位置,当输入的内容存在时,再去new HistoryWordModel实例

cke_717.png

所以我换了位置,当输入的内容存在时,再去new HistoryWordModel实例

cke_2634.png

import promptAction from '@ohos.promptAction'

@Entry
@Component
struct MainPage {
  @State historyWords: Array<HistoryWordModel> = []
  @State currentInputBoxContent: string = ''
  private historyWordIndex: number = 1
  build() {
    Column() {
      TextInput({ placeholder: '请输入', text: this.currentInputBoxContent })
        .onChange(event => {
          this.currentInputBoxContent = event
        })

      Button('搜索').width(200).onClick(() => {
        this.submitData()
      })

      ForEach(this.historyWords, (item: HistoryWordModel) => {
        Row() {
          Text(item.word).fontSize(16)
          Text().width(50)
          Text(item.index.toString())
        }
      })

    }
  }

  submitData() {
    if (this.currentInputBoxContent.length != 0) {
      let wordModel: HistoryWordModel = new HistoryWordModel(0, this.currentInputBoxContent)
      //标识本次搜索的关键词是否存在
      let exist: boolean = false

      //如果搜索关键词存在,记录其位置,如果发现其已经是第一个位置,则不进行排序刷新动作
      let existIndex: number = -1

      //判断搜索关键是否存在于历史搜索词列表中
      this.historyWords.forEach((item, index) => {
        if (item.word === wordModel.word) {
          //如果本次搜索关键词已经处于历史搜索词的第一个位置,不做删除动作
          if (index != 0) {
            //如果存在,先删除历史词列表中的这个关键词
            this.historyWords.splice(index, 1)
          }
          exist = true
          existIndex = index
        }
      });

      //本次搜索关键词在历史搜索词列表中处于第一个位置,因此不做任何额外处理
      //NOTE:真实场景中,这里除了重置状态,应该发起网络请求
      if (existIndex == 0) {
        promptAction.showToast({ message: '不需要刷新页面' })
        this.currentInputBoxContent = ''
        return
      }

      if (!exist) {
        wordModel.index = this.historyWordIndex++
        this.historyWords.push(wordModel)
      } else {
        this.historyWordIndex++
        this.historyWords.push(new HistoryWordModel(this.historyWordIndex, wordModel.word, wordModel.link))
      }

      //NOTE:这个就是中转排序的起始代码
      let Test: Array<HistoryWordModel> = []

      this.historyWords.forEach((item, index) => {
        Test.push(item)
      })

      // 根据 HistoryWordModel 对象的 index 属性进行降序排序(较大的 index 值排在前面)
      Test.sort((a: HistoryWordModel, b: HistoryWordModel) => {
        return b.index - a.index
      })

      // 清空原数组 this.historyWords 的内容
      this.historyWords.length = 0

      // 再重新把Test数组中的内容复制给 historyWords
      Test.forEach((item, index) => {
        this.historyWords.push(item)
      })
      //NOTE:这个就是中转排序的结束代码

      this.currentInputBoxContent = ''

    } else {
      promptAction.showToast({ message: '请输入关键词' })
    }
  }
}

class HistoryWordModel {
  static NextId: number = 0; // 静态变量
  id: number;
  index: number;
  word: string;
  link?: string;

  constructor(index: number, word: string, link?: string) {
    console.info('=====HistoryWordModel.NextId',HistoryWordModel.NextId)
    this.id = HistoryWordModel.NextId++;
    this.index = index;
    this.word = word;
    this.link = link;
  }
}

可能是我没表述明白,这个方案没能解决我的问题:
先搜索一个历史记录中存在的记录,再搜索一个新的词汇,这个新的词汇的排序位置不对。

在HarmonyOS中,搜索历史记录的排列顺序可以通过SearchResultSession类中的sort方法进行控制。默认情况下,搜索历史记录按照时间倒序排列,即最新的记录在最前面。如果需要自定义排序方式,可以通过实现Comparator接口来定义排序规则。

以下是一个示例代码,展示如何自定义搜索历史记录的排列顺序:

import { SearchResultSession, SearchResult } from '@ohos.data.search';

// 自定义排序规则
const customComparator = (a: SearchResult, b: SearchResult) => {
    // 按照搜索结果的某个属性进行排序,例如按照搜索次数
    return a.getExtra().searchCount - b.getExtra().searchCount;
};

// 获取搜索历史记录
const searchSession = new SearchResultSession();
searchSession.getSearchResults().then((results: SearchResult[]) => {
    // 使用自定义排序规则对结果进行排序
    results.sort(customComparator);
    // 处理排序后的结果
    // ...
});

在这个示例中,customComparator函数定义了按照搜索次数进行排序的规则。通过调用results.sort(customComparator),可以将搜索历史记录按照自定义的规则进行排序。

需要注意的是,SearchResult对象可能包含不同的属性,具体排序规则可以根据实际需求进行调整。

回到顶部