@@ -162,7 +162,13 @@ private function showDataOfTable(string $tableName, int $limitRows, int $limitFi
162162 $ thead = $ this ->db ->getFieldNames ($ tableName );
163163 $ this ->restoreDBPrefix ();
164164
165- $ this ->tbody = $ this ->makeTableRows ($ tableName , $ limitRows , $ limitFieldValue );
165+ // If there is a field named `id`, sort by it.
166+ $ sortField = null ;
167+ if (in_array ('id ' , $ thead , true )) {
168+ $ sortField = 'id ' ;
169+ }
170+
171+ $ this ->tbody = $ this ->makeTableRows ($ tableName , $ limitRows , $ limitFieldValue , $ sortField );
166172 CLI ::table ($ this ->tbody , $ thead );
167173 }
168174
@@ -203,13 +209,21 @@ private function makeTbodyForShowAllTables(array $tables): array
203209 return $ this ->tbody ;
204210 }
205211
206- private function makeTableRows (string $ tableName , int $ limitRows , int $ limitFieldValue ): array
207- {
212+ private function makeTableRows (
213+ string $ tableName ,
214+ int $ limitRows ,
215+ int $ limitFieldValue ,
216+ ?string $ sortField = null
217+ ): array {
208218 $ this ->tbody = [];
209219
210220 $ this ->removeDBPrefix ();
211221 $ builder = $ this ->db ->table ($ tableName );
212- $ rows = $ builder ->limit ($ limitRows )->get ()->getResultArray ();
222+ $ builder ->limit ($ limitRows );
223+ if ($ sortField !== null ) {
224+ $ builder ->orderBy ($ sortField , $ this ->sortDesc ? 'DESC ' : 'ASC ' );
225+ }
226+ $ rows = $ builder ->get ()->getResultArray ();
213227 $ this ->restoreDBPrefix ();
214228
215229 foreach ($ rows as $ row ) {
@@ -222,7 +236,7 @@ private function makeTableRows(string $tableName, int $limitRows, int $limitFiel
222236 $ this ->tbody [] = $ row ;
223237 }
224238
225- if ($ this ->sortDesc ) {
239+ if ($ sortField === null && $ this ->sortDesc ) {
226240 krsort ($ this ->tbody );
227241 }
228242
0 commit comments