diff --git a/src/schema-analyzer.ts b/src/schema-analyzer.ts index 7d641cb..89707f1 100644 --- a/src/schema-analyzer.ts +++ b/src/schema-analyzer.ts @@ -293,7 +293,12 @@ export type SimplifiedSchema = { function simplifiedSchema(fields: SchemaAnalysisFieldsMap): SimplifiedSchema { function finalizeSchemaFieldTypes(types: SchemaAnalysisFieldTypes): SimplifiedSchemaType[] { - return Object.values(types).map((type) => { + return Object.values(types).sort( + (a: SchemaAnalysisType, b: SchemaAnalysisType) => { + // Sort the types by what occurs most frequent first. + return b.count - a.count; + } + ).map((type: SchemaAnalysisType) => { return { bsonType: type.bsonType, // Note: `Object` is replaced with `Document`. ...(isArrayType(type) ? { diff --git a/test/simplified-schema.test.ts b/test/simplified-schema.test.ts index 532292c..28af5e6 100644 --- a/test/simplified-schema.test.ts +++ b/test/simplified-schema.test.ts @@ -7,11 +7,11 @@ import { allBSONTypesDoc } from './all-bson-types-fixture'; const docsFixture = [ { foo: 1, - bar: 'test' + bar: 25 }, { foo: 2, - bar: 25, + bar: 'test', baz: true }, {