我的模型有一个int main()
{
cout << "\n********************************************************************************\n" << endl;
// create stateHashTable with default size 101
hashT<stateData> stateHashTable(101);
// open file with just state name and state capital as specified
ifstream infile;
infile.open("stateData.txt");
// run the file through stateData to keep track of its information
stateData listedState;
infile >> listedState;
// use the information from the file to make the hash table
while (infile >> listedState)
{
// initializing the index using the hash function to determine the hash address of an item
// using the name of the state as the key to determine the hash address
int stateIndex = stateHashTable.hashFunc(listedState.getStateName());
// insert/add the state to the hash table using quadratic probing
stateHashTable.insert(stateIndex, listedState);
}
// close file so the operating system is notified resources are available
infile.close();
// initializing values to be run through functions
bool foundFirst;
int hashIndex;
string stateName;
stateData searchState;
// call function to print hash table before the user input state has been removed
stateHashTable.print();
cout << "\n\nEnter the name of a state: ";
cin >> stateName;
// setting hashIndex to the index result of running the input state through the hashFunc function
hashIndex = stateHashTable.hashFunc(stateName);
// setting searchState to the result of running the
// input state through stateData to keep track of information
searchState = stateData(stateName);
// search to see if user input state is in the hash table,
// then set foundFirst to true
stateHashTable.search(hashIndex, searchState, foundFirst);
// Function to determine whether the user input state is in the hash table at
// position hashIndex is true as well as foundSecond
if (foundFirst && stateHashTable.isItemAtEqual(hashIndex, searchState))
{
cout << "\nThe state and associated capital: " << endl;
stateHashTable.retrieve(hashIndex, searchState);
// print the user input state as well as the associated capital
cout << searchState << endl;
// remove the user input from the hash table
stateHashTable.remove(hashIndex, searchState);
// initializing the value to compare foundFirst to
bool foundSecond;
// search the hash table for the state and set foundSecond value
stateHashTable.search(hashIndex, searchState, foundSecond);
// Function to determine whether the user input state is in the hash table at
// position hashIndex is true as well as foundSecond
if (foundSecond && stateHashTable.isItemAtEqual(hashIndex, searchState))
{
cout << "Unable to remove state: " << searchState << endl;
}
else
{
cout << "\nSuccessfully removed " << stateName << " from the hash table. \n" << endl;
cout << "*************************************HASH TABLE*************" << endl;
cout << "STATE NAME" << ", " << "STATE CAPTIAL" << endl;
// call function to print hash table after user input state has been removed
stateHashTable.print();
cout << "\n**********************************************************" << endl;
cout << "\n";
}
}
else
{
cout << "\nUnable to find state in hash table to remove." << endl;
}
system("pause");
}
领域对象,该对象通过属性GroupOfEvents
与Event
领域对象有很多关系。每个events
都有一个Event
属性。
我有一个date
,我想按Results<GroupOfEvents>
中第一个事件的date
进行排序。如果我要对常规的swift数组进行排序,它将看起来像:
events
这是我对groupsOfEvents.sorted(by: { $0.events.first!.date > $1.events.first!.date })
进行排序的失败尝试:
Results<GroupOfEvents>
出现错误:
“无法在关键路径'events.first.date'上进行排序:属性'GroupOfEvents.events'是不受支持的类型'array'。'
我如何按照自己的方式对其进行排序?
您可能会问我的问题:
为什么要按第一个事件的日期排序?人们通常不会这样做。
实际上,groupsOfEvents.sorted(byKeyPath: "events.first.date")
包含具有相同日期的事件,因此可以使用GroupOfEvents
中任何事件的日期。
为什么不将events
属性移到date
?
是的,我可以这样做,但是我正在寻找替代方案。
为什么不摆脱GroupOfEvents
而只获取所有GroupOfEvents
然后按日期分组?
尽管Events
中的所有events
具有相同的日期。两个不同GroupOfEvents
的事件也可以具有相同的日期。还有其他与GroupOfEvents
不同的属性。
答案 0 :(得分:0)
目前,您不能这样做。应该在任何“领域限制列表”中对其进行描述,但事实并非如此。实际上,github上有一个针对此问题的问题:Support of keypath sorting for to-many relationships。
这是有关此问题的一些解决方法:
最好的解决方案是将firstEvent: Event
添加到GroupOfEvents
模型中。 Support sorting by keypath说,您可以按.sorted(byKeyPath: "firstEvent.date")
进行排序。但是它引入了额外的工作来同步firstEvent
和events.first
。
对于程序员而言,最便宜的方式可能对效率而言是最差的方式-强制转换为Array
,然后进行排序。非常简单,但是如您所知,领域仅在您开始使用它时才给您对象(即,他们将lazy
放置在可以使用的所有位置)。如果您没有大量数据-应该会很好。
此外,Realm还有不错的NSPredicate Cheatsheet,涵盖了有用的模式,并用粉红色的点标记了支持的案例。
答案 1 :(得分:0)
我不确定我是否正确理解了问题,但是[error] 7#7: *3 open() "/usr/share/nginx/html/somestupidrandomurl" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /somestupidrandomurl HTTP/1.1", host: "localhost"
类具有Results
函数,该函数接受sorted
(请参见documentation)。因此,您可以执行以下操作:
Sequence